Httpclent Access HTTPS ignores host checks and some authentication processes

Source: Internet
Author: User
HTTP  4.3 previous HttpClient httpclient = HttpClient1; Create TrustManager X509trustmanager XTM = new X509trustmanager () {public void checkclienttrusted (x509certificate [] chain, String authtype) throws certificateexception {} public void checkservertrusted (X50 9certificate[] chain, String authtype) throws certificateexception {} public X509Certificate
      [] Getacceptedissuers () {return new x509certificate[] {};
    }
    };
      
      try {sslcontext ctx = sslcontext.getinstance ("SSL");
      
      Use TrustManager to initialize the context, TrustManager is only used by SSL sockets Ctx.init (NULL, new trustmanager[] {XTM}, NULL);
      Sslsocketfactory SF = new Sslsocketfactory (CTX, sslsocketfactory.allow_all_hostname_verifier);
      Scheme sch = new scheme ("https", 443, SF);
      Httpclient.getconnectionmanager (). Getschemeregistry (). Register (Sch); Create HttpPost HttpGet HttpPost =New HttpGet (URL);
      Httppost.setheader ("Content-type", contexttype); 
      Perform POST requests HttpResponse response = Httpclient.execute (HttpPost);
       Get response entity httpentity entity = response.getentity ();
       BS = Ioutils.tobytearray (Entity.getcontent ());
    if (null!= entity) {Entityutils.consume (entity);//Consume response content} return BS;
    catch (Exception e) {e.printstacktrace (); 
    Finally {//close connection, free resources//Httpclient.getconnectionmanager (). Shutdown ();
  } return BS; 

} ========================================================================================================== after 4.3
Sslcontext Sslcontext;         try {             Sslcontext = new Sslcontextbuilder (). loadtrustmaterial (NULL,                      New TrustStrategy () {                        //Trust all                      public boolean istrusted (x509certificate[] chain,                               String authtype) throws Certificateexception {                      
   return true;                   


 }                }). Build ();         sslconnectionsocketfactory SSLSF = new SslconneCtionsocketfactory (                Sslcontext,                
Sslconnectionsocketfactory.allow_all_hostname_verifier);         httpclient = Httpclients.custom ()       

          setsslsocketfactory (SSLSF). build ();
        HttpPost httppost = new HttpPost (HTTPADDR2);        //httpclient Construction Completion   Construction request Parameters       
  list<namevaluepair> formparams = new arraylist<namevaluepair> ();
        Formparams.add (New Basicnamevaluepair ("Sender_code", spid));
        Formparams.add (New Basicnamevaluepair ("Trx_content", Base64str));         Formparams.add (New Basicnamevaluepair ("SIGNATURE", Md5str));
        urlencodedformentity uefentity;
        uefentity = new urlencodedformentity (formparams);
        httppost.setentity (uefentity);
        closeablehttpresponse response = Httpclient.execute (HttpPost);





         httpentity entity = response.getentity ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.