Required Jar:commons-logging-1.1.3.jar, Httpclient-4.3.1.jar, Httpcore-4.3.jar
PackageCom.onlyou.microfinance.common.util;Importjava.security.KeyManagementException;Importjava.security.NoSuchAlgorithmException;Importjava.security.cert.X509Certificate;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Map;ImportJavax.net.ssl.SSLContext;ImportJavax.net.ssl.TrustManager;ImportJavax.net.ssl.X509TrustManager;Importorg.apache.http.HttpEntity;Importorg.apache.http.HttpHeaders;ImportOrg.apache.http.HttpHost;ImportOrg.apache.http.HttpResponse;ImportOrg.apache.http.NameValuePair;Importorg.apache.http.client.entity.UrlEncodedFormEntity;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.client.methods.HttpPost;Importorg.apache.http.conn.ssl.SSLConnectionSocketFactory;Importorg.apache.http.impl.client.CloseableHttpClient;Importorg.apache.http.impl.client.HttpClients;ImportOrg.apache.http.message.BasicNameValuePair;/*** HTTPS encapsulation class, support GET, post * *@authorAdministrator **/ Public classHttpsutil {Private StaticCloseablehttpclient client=NULL; Private Staticcloseablehttpclient createhttpsclient () {if(client!=NULL){ returnclient; } Try{X509trustmanager x509mgr=NewX509trustmanager () {@Override Public voidcheckclienttrusted (x509certificate[] Xcs, string string) {} @Override /c1> Public voidcheckservertrusted (x509certificate[] Xcs, string string) {} @Override /c4> Publicx509certificate[] Getacceptedissuers () {return NULL; } }; Sslcontext Sslcontext= Sslcontext.getinstance ("TLS"); Sslcontext.init (NULL,NewTrustmanager[]{x509mgr},NULL); Sslconnectionsocketfactory SSLSF=Newsslconnectionsocketfactory (Sslcontext, sslconnectionsocketfactory.allow_all_hostname_verifier); Client=Httpclients.custom (). Setsslsocketfactory (SSLSF). build (); returnclient; } Catch(keymanagementexception e) {e.printstacktrace (); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } returnHttpclients.createdefault (); } Public Statichttpentity Dogetbyhttps (string URL, string host, String CacheControl, String contentType, String Acceptch Arset, string pragma, string accept, String acceptencoding, string referer)throwsException {closeablehttpclient client=createhttpsclient (); Httphost Httphost=NewHttphost (host, 443, "https"); HttpGet HttpGet=Newhttpget (URL); Httpget.addheader (Httpheaders.cache_control, CacheControl); Httpget.addheader (Httpheaders.content_type, ContentType); Httpget.addheader (Httpheaders.accept_charset, Acceptcharset); Httpget.addheader (Httpheaders.pragma, PRAGMA); Httpget.addheader (httpheaders.accept, ACCEPT); Httpget.addheader (httpheaders.accept_encoding, acceptencoding); Httpget.addheader (Httpheaders.user_agent,"mozilla/5.0 (Windows NT 6.2; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/46.0.2471.0 safari/537.36 "); Httpget.addheader (Httpheaders.referer, REFERER); HttpResponse Response=Client.execute (Httphost, HttpGet); Httpentity Entity=response.getentity (); if(NULL!=entity) { //String result = entityutils.tostring (httpentity); //byte[] data = Entityutils.tobytearray (httpentity); returnentity; } Else { return NULL; } } Public StaticHttpentity dogetbyhttps (string URL, string host, String ContentType, String referer)throwsException {returnDogetbyhttps (URL, host, "No-cache", ContentType, "Utf-8", "No-cache", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "gzip, deflate, SDCH", Referer); } Public Statichttpentity Dopostbyhttps (string URL, string host, String CacheControl, String contentType, String ACCEPTC Harset, string pragma, string accept, String acceptencoding, String referer, Map<string, object>Parammap) {Httphost Httphost=NewHttphost (host, 443, "https"); HttpPost Httprequst=Newhttppost (URL); Httprequst.addheader (Httpheaders.cache_control, CacheControl); Httprequst.addheader (Httpheaders.content_type, ContentType); Httprequst.addheader (Httpheaders.accept_charset, Acceptcharset); Httprequst.addheader (Httpheaders.pragma, PRAGMA); Httprequst.addheader (httpheaders.accept, ACCEPT); Httprequst.addheader (httpheaders.accept_encoding, acceptencoding); Httprequst.addheader (Httpheaders.user_agent,"mozilla/5.0 (Windows NT 6.2; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/46.0.2471.0 safari/537.36 "); Httprequst.addheader (Httpheaders.referer, REFERER); List<NameValuePair> params =NewArraylist<>(); if(Parammap! =NULL&&!Parammap.isempty ()) { for(String key:paramMap.keySet ()) {Params.add (NewBasicnamevaluepair (Key, (String) Parammap.get (key)); } } Try{httprequst.setentity (Newurlencodedformentity (params)); Closeablehttpclient Client=createhttpsclient (); HttpResponse HttpResponse=Client.execute (Httphost, httprequst); if(Httpresponse.getstatusline (). Getstatuscode () = = 200) {httpentity httpentity=httpresponse.getentity (); //String result = entityutils.tostring (httpentity); //byte[] data = Entityutils.tobytearray (httpentity); returnhttpentity; } } Catch(Exception e) {e.printstacktrace (); } return NULL; } Public StaticHttpentity dopostbyhttps (string URL, string host, String ContentType, String referer, map<string, object> Parammap)throwsException {returnDopostbyhttps (URL, host, "No-cache", ContentType, "Utf-8", "No-cache", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "gzip, deflate, SDCH", Referer, Parammap); }}
Invocation Example:
Httpentity loginentity = Httpsutil.dopostbyhttps (URL, "ipcrs.pbccrc.org.cn", "application/x-www-form-urlencoded", " https://*/page/login/loginreg.jsp "= Httpsutil.dogetbyhttps (URL, host," Image/jpeg "," Https://*/page/login /loginreg.jsp ");
HTTPS encapsulation class, support Get/post request