Sending HTTPS requests via Apache Commons HttpClient

Source: Internet
Author: User

1, send the POST request via HTTPS;

2, HTTPS security protocol using TLSv1.2;

3, using the proxy for HTTPS access;

4, the designated Content-type: application/x-www-form-urlencoded;

5, the HTTPS Request load client certificate (customer Certificate);

6. Ignore the checksum of the server-side certificate chain (server Certificate Chain) (Validate).

 Public Static voidMain (string[] args)throwsIOException, Unrecoverablekeyexception, Certificateexception, Keystoreexception, keymanagementexception {SSLC Onnectionsocketfactory socketfactory=getsocketfactory (); //Create a Closeablehttpclient objectCloseablehttpclient httpclient =Httpclients.custom (). Setsslsocketfactory (Socketfactory). build (); //specify the requested URL and create the HttpPost objectHttpPost HttpPost =NewHttpPost ("https://xxxx/yyyy"); //set up a proxy for a requestHttppost.setconfig (Requestconfig.custom (). SetProxy (NewHttphost ("host", 8080)) . Build ());                Httpentity entity; //set the ContentType of the request to application/x-www-form-urlencodedHttppost.addheader (Httpheaders.content_type, consts.http_request_contenttype_form); //Building the POST contentList<basicnamevaluepair> Nvps =NewArraylist<>(); Nvps.add (NewBasicnamevaluepair ("Amount", "1.00")); Entity=Newurlencodedformentity (Nvps, Consts.charset_utf8);        Httppost.setentity (entity); Closeablehttpresponse Response=NULL; Try {            //Send RequestResponse =Httpclient.execute (HttpPost); //Get Response ContentHttpentity entity1 =response.getentity ();        System.out.println (entityutils.tostring (entity1)); } finally {            if(NULL!=response)            {Response.close (); }            if(NULL!=httpclient)            {Httpclient.close (); }        }    }    //ignoring authentication on the server-side certificate chain    Private StaticTrustManager gettrustmanagers () {return NewX509trustmanager () { Publicx509certificate[] Getacceptedissuers () {return NULL; }             Public voidcheckclienttrusted (x509certificate[] certs, String authtype) {} Public voidcheckservertrusted (x509certificate[] certs, String authtype) {}}; }    Private StaticSslconnectionsocketfactory Getsocketfactory ()throwsIOException, Keystoreexception, Certificateexception, Unrecoverablekeyexception, keymanagementexception {SSLC        Ontext Sslcontext; Try {            //KeyStore used to store client certificatesKeyStore KeyStore = keystore.getinstance ("PKCS12"); FileInputStream instream=NewFileInputStream (NewFile ("D:\\TEST.P12")); Try{keystore.load (instream,"passwd". ToCharArray ()); } finally{instream.close (); }                        //load the client certificate and set the HTTPS security protocol to TLSv1.2Sslcontext = Sslcontexts.custom (). Loadkeymaterial (KeyStore, "passwd". ToCharArray ()). Useprotocol ("TLSv1.2"). build (); } Catch(nosuchalgorithmexception e) {return NULL; }        Try{sslcontext.init (NULL,NewTrustmanager[]{gettrustmanagers ()},Newjava.security.SecureRandom ()); } Catch(keymanagementexception e) {return NULL; }        return Newsslconnectionsocketfactory (Sslcontext); }

Sending HTTPS requests via Apache Commons HttpClient

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.