Implementing an HTTPS GET request

Source: Internet
Author: User

Required Jar:commons-logging-1.1.3.jar, Httpclient-4.3.1.jar, Httpcore-4.3.jar

 Packagecom.ljq.test;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.security.cert.CertificateException;Importjava.security.cert.X509Certificate;ImportJavax.net.ssl.HostnameVerifier;ImportJavax.net.ssl.SSLContext;Importjavax.net.ssl.SSLSession;ImportJavax.net.ssl.TrustManager;ImportJavax.net.ssl.X509TrustManager;ImportOrg.apache.http.HttpHost;ImportOrg.apache.http.HttpResponse;Importorg.apache.http.client.HttpClient;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.conn.routing.HttpRoute;ImportOrg.apache.http.conn.scheme.Scheme;ImportOrg.apache.http.conn.scheme.SchemeRegistry;Importorg.apache.http.conn.ssl.SSLSocketFactory;Importorg.apache.http.impl.client.DefaultHttpClient;ImportOrg.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;ImportOrg.apache.http.params.CoreConnectionPNames;/** *  * @authorAdministrator **/ Public classImagetest { Public Static voidMain (string[] args)throwsIOException {fileoutputstream OutStream=NULL; String URL= "https://ipcrs.pbccrc.org.cn/imgrc.do?a=1439538233825"; String Host= "ipcrs.pbccrc.org.cn"; Try{InputStream instream=Dogetbyhttps (url,host);        System.out.println (instream); } Catch(Exception e) {e.printstacktrace (); }     }    Private Static classTrustanytrustmanagerImplementsX509trustmanager { Public voidcheckclienttrusted (x509certificate[] chain, String authtype)throwscertificateexception {} Public voidcheckservertrusted (x509certificate[] chain, String authtype)throwscertificateexception {} Publicx509certificate[] Getacceptedissuers () {return Newx509certificate[] {}; }    }    Private Static classTrustanyhostnameverifierImplementsHostnameverifier { Public BooleanVerify (String hostname, sslsession session) {return true; }    }         Public StaticInputStream dogetbyhttps (string url, string shost)throwsException {LongTime =System.currenttimemillis (); HttpResponse Response=NULL; Defaulthttpclient Client=NULL; InputStream Oinput=NULL; Sslcontext Octx= Sslcontext.getinstance ("TLS"); Octx.init (NULL,Newtrustmanager[]{NewTrustanytrustmanager ()},NULL); Sslsocketfactory socketfactory=Newsslsocketfactory (Octx, sslsocketfactory.allow_all_hostname_verifier); Schemeregistry Schemeregistry=NewSchemeregistry (); Schemeregistry.register (NewScheme ("https", 443, socketfactory)); Threadsafeclientconnmanager Connmanager=NewThreadsafeclientconnmanager (schemeregistry); Connmanager.setdefaultmaxperroute (20); Httphost Host=NewHttphost (shost); Connmanager.setmaxforroute (NewHttproute (host), 20); Client=Newdefaulthttpclient (Connmanager); HttpGet Oget=Newhttpget (URL); Oget.setheader ("Accept-charset", "Utf-8"); Oget.setheader ("Content-type", "application/x-www-form-urlencoded");        Settimedout (client); Response=Client.execute (Oget); Oinput=response.getentity (). getcontent (); returnOinput; }        /*** Set timedout * *@paramConnection *@return     */    Private Static voidsettimedout (HttpClient HttpClient) {httpclient.getparams (). Setparameter (Coreconnectionpnames.connection_tim Eout,30000); Httpclient.getparams (). Setparameter (Coreconnectionpnames.so_timeout,20000); }}

Implementing an HTTPS GET request

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.