Recently, we need to do a wrapper on the customer's interface, and then for their own company system calls, the customer interface is implemented with the HTTP URL, I would like to use the HttpClient package to request, and because the requested URL is HTTPS, in order to avoid the need for a certificate, So you inherit the Defaulthttpclient class with a class, ignoring the checksum process.
1. Write a sslclient class, inherit to HttpClient
Package Com.pcmall.service.sale.miaomore.impl;
Import java.security.cert.CertificateException;
Import Java.security.cert.X509Certificate;
Import Javax.net.ssl.SSLContext;
Import Javax.net.ssl.TrustManager;
Import Javax.net.ssl.X509TrustManager;
Import Org.apache.http.conn.ClientConnectionManager;
Import Org.apache.http.conn.scheme.Scheme;
Import Org.apache.http.conn.scheme.SchemeRegistry;
Import Org.apache.http.conn.ssl.SSLSocketFactory;
Import org.apache.http.impl.client.DefaultHttpClient;
The httpclient public class Sslclient extends for HTTPS requests defaulthttpclient{public sslclient () throws exception{
Super ();
Sslcontext CTX = sslcontext.getinstance ("TLS"); X509trustmanager TM = new X509trustmanager () {@Override public void checkclienttrusted (X509certificat E[] chain, String authtype) throws certificateexception {} @Override public V OID checkservertrusted (x509certificate[] ChAin, String authtype) throws certificateexception {} @Override public x509cer
Tificate[] Getacceptedissuers () {return null;
}
};
Ctx.init (NULL, new TRUSTMANAGER[]{TM}, NULL);
Sslsocketfactory SSF = new Sslsocketfactory (ctx,sslsocketfactory.allow_all_hostname_verifier);
Clientconnectionmanager CCM = This.getconnectionmanager ();
schemeregistry sr = Ccm.getschemeregistry ();
Sr.register (New Scheme ("https", 443, SSF)); }
}
2. Write a class that uses httpclient to send a POST request
Package Com.pcmall.service.sale.miaomore.impl;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Map.Entry;
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.NameValuePair;
Import org.apache.http.client.HttpClient;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
Import Org.apache.http.client.methods.HttpPost;
Import Org.apache.http.message.BasicNameValuePair;
Import Org.apache.http.util.EntityUtils; * * Tool class with httpclient for post requests/public class Httpclientutil {public string doPost (string url,map<string,str
Ing> map,string CharSet) {httpclient httpclient = null;
HttpPost httppost = null;
String result = null;
try{httpclient = new Sslclient ();
HttpPost = new HttpPost (URL);
Set parameter list<namevaluepair> List = new arraylist<namevaluepair> (); Iterator iterator = Map.entryset (). iterator ();
while (Iterator.hasnext ()) {entry<string,string> Elem = (entry<string, string>) Iterator.next ();
List.add (New Basicnamevaluepair (Elem.getkey (), Elem.getvalue ()));
} if (List.size () > 0) {urlencodedformentity entity = new urlencodedformentity (list,charset);
Httppost.setentity (entity);
} HttpResponse response = Httpclient.execute (HttpPost);
if (response!= null) {httpentity resentity = response.getentity ();
if (resentity!= null) {result = Entityutils.tostring (Resentity,charset);
}}catch (Exception ex) {ex.printstacktrace ();
return result; }
}
3. To invoke the test code for the POST request
Package Com.pcmall.service.sale.miaomore.impl;
Import Java.util.HashMap;
Import Java.util.Map;
Testing the interface public class Testmain {private String URL = "https://xxx.xxx.xxx/";
Private String charset = "Utf-8";
Private Httpclientutil httpclientutil = null;
Public Testmain () {httpclientutil = new httpclientutil ();
public void Test () {String httporgcreatetest = url + ' xxx/xxx/delivery ';
map<string,string> Createmap = new hashmap<string,string> ();
Createmap.put ("Delivery_code", "1d1qz222z22sm21a");
Createmap.put ("timestamp", "1479198840000");
Createmap.put ("sign", "F2109c333f3eade929f932e89703fa0f683d43eb");
String Httporgcreatetestrtn = Httpclientutil.dopost (Httporgcreatetest,createmap,charset);
SYSTEM.OUT.PRINTLN ("Result:" +httporgcreatetestrtn);
public static void Main (string[] args) {Testmain main = new Testmain ();
Main.test (); }
}
The beginning is not very understand the use of basicnamevaluepair, and then slowly groped for a moment, found that Basicnamevaluepair is a class to store key-value pairs, when adding new key and value values, it will automatically replace the format of HTTP, = and & Symbol , such as https://xxx.xxx.xxx/xxx/xxxx/delivery?delivery_code=DQZZSM2A×tamp=1479198840000&sign= F209c33feade99f93e8970fa0f68d3eb, we do not have their own stitching and matching, personally think it is very convenient to use and high accuracy, hope to help everyone!
The above is a small series for everyone to bring Java using httpclient for POST request (HTTPS) instance of all the content, I hope that we support cloud-Habitat Community ~