HttpClient4.3.6 for HTTPS access

Source: Internet
Author: User

 Packagehttptest;Importjava.io.IOException;ImportJava.nio.charset.Charset;Importjava.security.KeyManagementException;Importjava.security.KeyStoreException;Importjava.security.NoSuchAlgorithmException;Importjava.security.cert.X509Certificate;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Map;ImportJava.util.logging.Level;ImportJava.util.logging.Logger;ImportJavax.net.ssl.SSLContext;Importorg.apache.http.HttpEntity;ImportOrg.apache.http.NameValuePair;Importorg.apache.http.client.entity.UrlEncodedFormEntity;ImportOrg.apache.http.client.methods.CloseableHttpResponse;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.client.methods.HttpPost;Importorg.apache.http.conn.ssl.SSLConnectionSocketFactory;ImportOrg.apache.http.conn.ssl.SSLContextBuilder;ImportOrg.apache.http.conn.ssl.TrustStrategy;Importorg.apache.http.impl.client.CloseableHttpClient;Importorg.apache.http.impl.client.HttpClients;ImportOrg.apache.http.message.BasicNameValuePair;Importorg.apache.http.util.EntityUtils;/** * @authorYan * @date 2016-6-9 11:03:04 *@versionV1.0 * @desc*/ Public classHttpsutil { Public Static FinalString Get (FinalString URL,FinalMap<string, object>params) {StringBuilder SB=NewStringBuilder (""); if(NULL! = Params &&!Params.isempty ()) {            inti = 0;  for(String key:params.keySet ()) {if(i = = 0) {sb.append ("?"); } Else{sb.append ("&"); } sb.append (Key). Append ("="). Append (Params.get (key)); I++; }} closeablehttpclient httpClient=Createsslclientdefault (); Closeablehttpresponse Response=NULL; HttpGet Get=NewHttpGet (URL +sb.tostring ()); String result= ""; Try{Response=Httpclient.execute (GET); if(Response.getstatusline (). Getstatuscode () = = 200) {httpentity entity=response.getentity (); if(NULL!=entity) {Result= Entityutils.tostring (Entity, "UTF-8"); }            }        } Catch(IOException ex) {Logger.getlogger (httpsutil.class. GetName ()). log (Level.severe,NULL, ex); } finally {            if(NULL!=response) {                Try{Entityutils.consume (response.getentity ()); } Catch(IOException ex) {Logger.getlogger (httpsutil.class. GetName ()). log (Level.severe,NULL, ex); }            }        }        returnresult; }     Public Static FinalString Post (FinalString URL,FinalMap<string, object>params) {closeablehttpclient httpClient=Createsslclientdefault (); HttpPost Post=Newhttppost (URL); Closeablehttpresponse Response=NULL; if(NULL! = Params &&!Params.isempty ()) {List<NameValuePair> nvplist =NewArraylist<namevaluepair>();  for(Map.entry<string, object>Entry:params.entrySet ()) {Namevaluepair NVP=NewBasicnamevaluepair (Entry.getkey (), Entry.getvalue (). toString ());            Nvplist.add (NVP); } post.setentity (NewUrlencodedformentity (Nvplist, Charset.forname ("UTF-8"))); The String result= ""; Try{Response=Httpclient.execute (POST); if(Response.getstatusline (). Getstatuscode () = = 200) {httpentity entity=response.getentity (); if(NULL!=entity) {Result= Entityutils.tostring (Entity, "UTF-8"); }            }        } Catch(IOException ex) {Logger.getlogger (httpsutil.class. GetName ()). log (Level.severe,NULL, ex); } finally {            if(NULL!=response) {                Try{Entityutils.consume (response.getentity ()); } Catch(IOException ex) {Logger.getlogger (httpsutil.class. GetName ()). log (Level.severe,NULL, ex); }            }        }        returnresult; }    Private Staticcloseablehttpclient Createsslclientdefault () {Sslcontext sslcontext; Try{Sslcontext=NewSslcontextbuilder (). Loadtrustmaterial (NULL,NewTruststrategy () {//Trust all@Override Public Booleanistrusted (x509certificate[] Xcs, string string) {return true;            }}). Build (); Sslconnectionsocketfactory SSLSF=Newsslconnectionsocketfactory (Sslcontext); returnHttpclients.custom (). Setsslsocketfactory (SSLSF). build (); } Catch(Keystoreexception ex) {Logger.getlogger (httpsutil.class. GetName ()). log (Level.severe,NULL, ex); } Catch(NoSuchAlgorithmException ex) {Logger.getlogger (httpsutil.class. GetName ()). log (Level.severe,NULL, ex); } Catch(Keymanagementexception ex) {Logger.getlogger (httpsutil.class. GetName ()). log (Level.severe,NULL, ex); }        returnHttpclients.createdefault (); }     Public Static voidMain (string[] args) {System.out.println ("Result:" + Get ("https://github.com/",NULL)); }}

HttpClient4.3.6 for HTTPS access

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.