Pre-start Preparation:
Requires an Access certificate under Https://XXXXX to be saved as a CER file.
Base code:
1, refer to two articles:
HTTP://BLOG.CSDN.NET/LIUXIAOSHUANG002/ARTICLE/DETAILS/51955020 is used to determine whether it is empty
http://blog.csdn.net/liuxiaoshuang002/article/details/51955031 Compare objects for equality
2, after the request to return the entity httpresponseentity detailed code is as follows:
Package com.gj5u.publics.util.httpweb;
Import Java.util.Map;
/** * Get content entity * @author Rex */public class Httpresponseentity {private map<string,string> resheaders;
Private String httpbody;
Private String Statusline;
Private String errmessage; /** * Header information * @return header information/public map<string, String> getresheaders () {RET
Urn Resheaders; /** * Header information * @param resheaders header information/public void Setresheaders (map<string, String> ;
resheaders) {this.resheaders = resheaders;
}/** * Body content * @return Body content/Public String gethttpbody () {return httpbody;
}/** * Body content * @param httpbody body content/public void Sethttpbody (String httpbody) {
This.httpbody = Httpbody;
/** * Status line * @return status Line/public String Getstatusline () {return statusline; }
/** * Status line * @param statusline status Line/public void Setstatusline (String statusline) {th
Is.statusline = Statusline;
Public String Geterrmessage () {return errmessage;
} public void Seterrmessage (String errmessage) {errmessage = Errmessage;
}
}
Detailed code on the request
Package com.gj5u.publics.util.httpweb;
Import Java.io.BufferedReader;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.util.HashMap;
Import Java.util.Map;
Import Org.apache.http.Header;
Import org.apache.http.HttpEntity;
Import Org.apache.http.client.config.RequestConfig;
Import Org.apache.http.client.methods.CloseableHttpResponse;
Import Org.apache.http.client.methods.HttpDelete;
Import Org.apache.http.client.methods.HttpGet;
Import Org.apache.http.client.methods.HttpHead;
Import org.apache.http.client.methods.HttpOptions;
Import Org.apache.http.client.methods.HttpPatch;
Import Org.apache.http.client.methods.HttpPost;
Import Org.apache.http.client.methods.HttpPut;
Import Org.apache.http.client.methods.HttpTrace;
Import org.apache.http.entity.StringEntity;
Import org.apache.http.impl.client.CloseableHttpClient;
Import Cn.ava.publics.util.EmptyUtil;
Import Cn.ava.publics.util.EqualsUtil;
The public class Myhttpsconn {//[start] attribute defines the URL of the/* request * * Private String Requrl;
Private String Webcerpath;
Private map<string, string> reqheaderkv; /** * HTTP Request mode * * @author REX */public enum HttpMethod {POST, get, DELETE,
Head, PATCH, TRACE, OPTIONS, put} public String Getrequrl () {return requrl;
Public String Getwebcerpath () {return webcerpath;
Public map<string, String> getreqheaderkv () {return reqheaderkv; }//[end]/** * initialization * * @param URL * Request URL * @param Cerpath *
Certificate path * @param cerpwd * Certificate Password/public myhttpsconn (string URL, string cerpath) {
Requrl = URL;
Webcerpath = Cerpath; Public httpresponseentity Sendhttps (HttpMethod method, map<string, string> Parameters, map<string, STR Ing> Header, String body, string[] headerfield)
{HttpPost HttpPost = new HttpPost (Requrl);
HttpGet httpget = new HttpGet (Requrl);
Httpdelete httpdelete = new Httpdelete (Requrl);
Httphead httphead = new Httphead (Requrl);
Httppatch Httppatch = new Httppatch (Requrl);
HttpTrace HttpTrace = new HttpTrace (Requrl);
Httpoptions httpoptions = new Httpoptions (Requrl);
Httpput httpput = new Httpput (Requrl);
1. The URL parameter is written to the URL if (emptyutil.isnotempty (Parameters)) {String pvalue = "";
For (map.entry<string, string> entry:Parameters.entrySet ()) {if (pvalue!= "")
{pvalue + = "&";
} pvalue + = Entry.getkey () + "=" + Entry.getvalue (); } if (Pvalue!= "") {this.
Requrl + = "?" + pvalue; }//2, write header to header if (Emptyutil.isnotempty headER) {for (map.entry<string, string> entry:Header.entrySet ()) {I F (Equalsutil.objequals (Httpmethod.post, method)) {Httppost.setheader (Entry.getkey ()),
Entry.getvalue ()); else if (Equalsutil.objequals (Httpmethod.get, method)) {Httpget.set
Header (Entry.getkey (), Entry.getvalue ()); else if (Equalsutil.objequals (Httpmethod.delete, method)) {Httpdele
Te.setheader (Entry.getkey (), Entry.getvalue ()); else if (Equalsutil.objequals (Httpmethod.head, method)) {Httphead.s
Etheader (Entry.getkey (), Entry.getvalue ()); else if (Equalsutil.objequals (Httpmethod.options, method)) {httpopt
Ions.setheader (Entry.getkey (), Entry.getvalue ()); else if (Equalsutil.objequals (Httpmethod.patch, method)) {
Httppatch.setheader (Entry.getkey (), Entry.getvalue ()); else if (Equalsutil.objequals (Httpmethod.put, method)) {Httpput.set
Header (Entry.getkey (), Entry.getvalue ()); else if (Equalsutil.objequals (Httpmethod.trace, method)) {HttpTrace
. SetHeader (Entry.getkey (), Entry.getvalue ());
{}}//3, Request message body settings httpentity entity = new stringentity (Bodies, "UTF-8");
if (Equalsutil.objequals (Httpmethod.post, method)) {httppost.setentity (entity);
else if (Equalsutil.objequals (Httpmethod.patch, method)) {httppatch.setentity (entity);
else if (Equalsutil.objequals (Httpmethod.put, method)) { Httpput.setentity (entity); } sslsocketfactoryload.loadsslsocketfactory (this.
Webcerpath);
Httpresponseentity responseentity = new httpresponseentity (); Boolean Ishttps = this.
Requrl.startswith ("https");
Closeablehttpclient httpclient = httpclientconstructor.gethttpclient (ISHTTPS); if (Ishttps) {Requestconfig requestconfig = Requestconfig.custom (). SetSocketTimeout (30000). SetConnect
Timeout (30000). Build ()//Set request and transmission timeout if (Equalsutil.objequals (Httpmethod.post, method)) {
Httppost.setconfig (Requestconfig); else if (Equalsutil.objequals (Httpmethod.get, method)) {Httpget.setconfig (reque
Stconfig); else if (Equalsutil.objequals (Httpmethod.delete, method)) {Httpdelete.setconfig
(Requestconfig);
else if (Equalsutil.objequals (Httpmethod.head, method) {httphead.setconfig (requestconfig); else if (Equalsutil.objequals (Httpmethod.options, method)) {httpoptions.setconf
IG (Requestconfig); else if (Equalsutil.objequals (Httpmethod.patch, method)) {Httppatch.setconfig (R)
Equestconfig); else if (Equalsutil.objequals (Httpmethod.put, method)) {Httpput.setconfig (reque
Stconfig); else if (Equalsutil.objequals (Httpmethod.trace, method)) {Httptrace.setconfig (R)
Equestconfig);
} closeablehttpresponse responsebody = null; try {if (Equalsutil.objequals (Httpmethod.post, method)) {responsebody =
Httpclient.execute (HttpPost);
else if (Equalsutil.objequals (Httpmethod.get, method)) { Responsebody = Httpclient.execute (HttpGet); else if (Equalsutil.objequals (Httpmethod.delete, method)) {responsebody = Httpc
Lient.execute (Httpdelete); else if (Equalsutil.objequals (Httpmethod.head, method)) {responsebody = Httpcli
Ent.execute (Httphead); else if (Equalsutil.objequals (Httpmethod.options, method)) {responsebody = http
Client.execute (httpoptions); else if (Equalsutil.objequals (Httpmethod.patch, method)) {responsebody = HTTPCL
Ient.execute (Httppatch); else if (Equalsutil.objequals (Httpmethod.put, method)) {responsebody = Httpclie
Nt.execute (Httpput); else if (Equalsutil.objequals (Httpmethod.trace, method)) {responsebody = HTTPCL Ient.execute (HttpTrace);
} responseentity = Dealwithresponse (responsebody);
REQHEADERKV = new hashmap<string, string> (); For (map.entry<string, string> entry:responseEntity.getResHeaders (). EntrySet ()) {if (
Emptyutil.isnotempty (Headerfield)) {for (int i = 0; i < headerfield.length; i++)
{if (Equalsutil.stringequals (Entry.getkey (), Headerfield[i], false)
{Reqheaderkv.put (Entry.getkey (), Entry.getvalue ()); }} else {Reqheaderkv.put (entry
. Getkey (), Entry.getvalue ()); catch (Exception e) {responseentity.seterrmessage ("Send Request FAI
led.\n "+ e.getmessage ());
E.printstacktrace ();
} Finally {try {httpclient.close (); catch (Exception e) {responseentity.seterrmessage ("HttpClient closed failed.\n")
+ e.getmessage ());
} return responseentity; Private Httpresponseentity Dealwithresponse (Closeablehttpresponse responsebody) {BufferedReader re
Ader = null;
Httpresponseentity responseentity = new httpresponseentity ();
try {header[] resheaders = Responsebody.getallheaders ();
map<string, string> resheadersarray = new hashmap<string, string> (); for (int i = 0; i < resheaders.length i++) {resheadersarray.put (Resheaders[i].getname ()),
Resheaders[i].getvalue ());
} responseentity.setresheaders (Resheadersarray); Responseentity.setstatusline (New String (responsebody.getstatusline(). ToString (). GetBytes ("Iso-8859-1"), "UTF-8"));
InputStream content = Responsebody.getentity (). getcontent ();
reader = new BufferedReader (content, "UTF-8") (new InputStreamReader);
String line = null;
StringBuilder Strbuffer = new StringBuilder ();
while (line = Reader.readline ())!= null) {strbuffer.append (line + \ n));
} responseentity.sethttpbody (Strbuffer.tostring ()); catch (Exception e) {responseentity.seterrmessage ("Failed to analysing HTTP response.\n" +
E.getmessage ());
Finally {try {reader.close (); catch (Exception e) {responseentity.seterrmessage ("reader close error. \ n" + E.)
GetMessage ());
} return responseentity;
}
}
How to use
Myhttpsconn httpconn = new Myhttpsconn (Httpsurl, cerpath); The requested URL and certificate path
httpresponseentity httpentry = httpconn. Sendhttps (Httpmethod.get, Parameters, Reqheader, Body,getheader);