[HttpClient] SSL bidirectional instance

Source: Internet
Author: User

 Packagecom.jerry.httpclient;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.security.KeyStore;ImportJavax.net.ssl.SSLContext;Importorg.apache.http.HttpEntity;ImportOrg.apache.http.client.methods.CloseableHttpResponse;ImportOrg.apache.http.client.methods.HttpGet;Importorg.apache.http.conn.ssl.SSLConnectionSocketFactory;Importorg.apache.http.conn.ssl.SSLContexts;ImportOrg.apache.http.conn.ssl.TrustSelfSignedStrategy;Importorg.apache.http.impl.client.CloseableHttpClient;Importorg.apache.http.impl.client.HttpClients;Importorg.apache.http.util.EntityUtils;/** *  * @authorJerry * @date February 12, 2015 PM 11:38:33*/ Public classSsldemo { Public Static FinalString Key_store_type_jks = "JKS";  Public Static FinalString KEY_STORE_TYPE_P12 = "PKCS12";  Public Static voidMain (string[] args)throwsexception{KeyStore KeyStore= Keystore.getinstance (KEY_STORE_TYPE_P12);//server sent to browser KeyStoreKeyStore Truststore = keystore.getinstance (KEY_STORE_TYPE_JKS);//KeyStore The browser sends to the serverFileInputStream input =NewFileInputStream (NewFile ("D:\\tomcat.keystore")); FileInputStream Keyinput=NewFileInputStream ("D:\\MYKEY.P12"); Truststore.load (Input,"123456". ToCharArray ()); Keystore.load (Keyinput,"123456". ToCharArray ()); /** Create a sslcontext with 2 KeyStore*/Sslcontext Sslcontext=Sslcontexts.custom (). USETLS (). Loadtrustmaterial (Truststore,Newtrustselfsignedstrategy ()). Loadkeymaterial (KeyStore,"123456". ToCharArray ()). build (); //Create a socket factory with SllcontextSslconnectionsocketfactory sslfactory =Newsslconnectionsocketfactory (Sslcontext); Closeablehttpclient Client=Httpclients.custom (). Setsslsocketfactory (Sslfactory). build (); HttpGet Get=NewHttpGet ("Https://localhost:8443/services"); System.out.println ("Executing request:" +get.getrequestline ()); Closeablehttpresponse Response=Client.execute (GET); Httpentity Entity=response.getentity (); if(Entity! =NULL) {System.out.println ("Response content Lenght:" +entity.getcontentlength ());        } System.out.println (Entityutils.tostring (entity));    Entityutils.consume (entity); }}

[HttpClient] SSL bidirectional instance

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.