1. To avoid the need for a certificate, use a class to inherit the Defaulthttpclient class, ignoring the checksum process.
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; /** * httpclient * @ClassName for HTTPS requests: Sslclient * @Description: TODO * @author Devin <xxx> * @date 2017
February 7 pm 1:42:07 * */public class Sslclient extends Defaulthttpclient {public sslclient () throws
Super ();
Sslcontext CTX = sslcontext.getinstance ("TLS"); X509trustmanager TM = new X509trustmanager () {@Override public void checkclienttrusted (X5
09certificate[] chain, String authtype) throws Certificateexception { @Override public void checkservertrusted (x509certificate[] chain, String authtype) throws certificateexception {} @Override public X50
9certificate[] 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. Create a tool class that uses httpclient to send post requests
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.StatusLine;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.entity.StringEntity;
Import Org.apache.http.message.BasicHeader;
Import Org.apache.http.util.EntityUtils; /** * @ClassName: Httpclientutil * @Description: TODO * @author Devin <xxx> * @dat for post requests using httpclient E February 7, 2017 pm 1:43:38 * */public class Httpclientutil {@SuppressWarnings ("resource") public static String D
Opost (String url,string jsonstr,string charset) {httpclient = null;
HttpPost httppost = null;
String result = null;
try{httpclient = new Sslclient ();
HttpPost = new HttpPost (URL);
Httppost.addheader ("Content-type", "Application/json");
Stringentity se = new stringentity (JSONSTR);
Se.setcontenttype ("Text/json"); SE.setcontentencoding (New Basicheader ("Content-type", "Application/json"));
Httppost.setentity (SE);
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. Test code
public static void Main (string[] args) {
String url = ' https://192.168.1.101/xxx ';
String jsonstr = "{xxx}";
String httporgcreatetestrtn = httpclientutil.dopost (URL, jsonstr, "Utf-8");
}