How to use HTTPS in Httpclient_httpclient

Source: Internet
Author: User

ImportJavax.net.ssl.SSLContext;ImportJavax.net.ssl.TrustManager;ImportJavax.net.ssl.X509TrustManager;Importjava.security.cert.CertificateException;Importjava.security.cert.X509Certificate;Importorg.apache.http.client.ClientProtocolException;Importorg.apache.http.client.HttpClient;ImportOrg.apache.http.client.ResponseHandler;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.conn.ClientConnectionManager;ImportOrg.apache.http.conn.scheme.Scheme;ImportOrg.apache.http.conn.scheme.SchemeRegistry;Importorg.apache.http.conn.scheme.SchemeSocketFactory;Importorg.apache.http.conn.ssl.SSLSocketFactory;ImportOrg.apache.http.impl.client.BasicResponseHandler;ImportOrg.apache.http.impl.client.ClientParamsStack;Importorg.apache.http.impl.client.DefaultHttpClient;ImportOrg.apache.http.params.DefaultedHttpParams;ImportOrg.apache.http.params.HttpParams; Public classHttpclienttest { Public Static voidMain (String args[]) {Try{HttpClient HttpClient=Newdefaulthttpclient (); //Secure Protocol implementation.Sslcontext CTX = sslcontext.getinstance ("SSL"); //implementation of a trust manager for X509 certificatesX509trustmanager TM =NewX509trustmanager () { Public voidcheckclienttrusted (x509certificate[] Xcs, string string)throwscertificateexception {} Public voidcheckservertrusted (x509certificate[] Xcs, string string)throwscertificateexception {} Publicx509certificate[] Getacceptedissuers () {return NULL;            }            }; Ctx.init (NULL,NewTrustmanager[] {TM},NULL); Sslsocketfactory SSF=Newsslsocketfactory (CTX); Clientconnectionmanager CCM=Httpclient.getconnectionmanager (); //register HTTPS protocol in HttpClient ' s scheme Registryschemeregistry sr =Ccm.getschemeregistry (); Sr.register (NewScheme ("https", 443, SSF)); HttpGet HttpGet=NewHttpGet (""); Httpparams params=Httpclient.getparams (); Params.setparameter ("param1", "paramValue1");            Httpget.setparams (params); System.out.println ("REQUEST:" +Httpget.geturi ()); ResponseHandler ResponseHandler=NewBasicresponsehandler ();            String responsebody; Responsebody=Httpclient.execute (HttpGet, ResponseHandler);            System.out.println (responsebody); //Create a response handler        } Catch(nosuchalgorithmexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(clientprotocolexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(Exception ex) {ex.printstacktrace (); }    }}

How to use HTTPS in Httpclient_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.