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