Java HTTPS client trusts all certificates

Source: Internet
Author: User

Package httpsclient;

Import java.io.IOException;
Import java.util.List;
Import java.util.ArrayList;


Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import org.apache.http.client.ClientProtocolException;
Import Org.apache.http.client.ResponseHandler;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.impl.client.CloseableHttpClient;
Import org.apache.http.impl.client.HttpClients;
Import Org.apache.http.util.EntityUtils;
Import Org.apache.http.client.entity.EntityBuilder;
Import Org.apache.http.conn.ssl.SSLConnectionSocketFactory;
Import Java.security.KeyStore;
Import if.*;

Import Javax.net.ssl.SSLContext;
Import org.apache.http.conn.ssl.SSLContexts;
Import Org.apache.http.conn.ssl.TrustSelfSignedStrategy;

Import Com.google.gson.Gson;


public class Myhttp {
public static void Main (string[] args) throws exception{

KeyStore Truststore = keystore.getinstance (Keystore.getdefaulttype ());
Sslcontext Sslcontext = Sslcontexts.custom (). Loadtrustmaterial (Truststore, New Trustselfsignedstrategy ()). Build ( );
Sslconnectionsocketfactory SSLSF = new Sslconnectionsocketfactory (sslcontext,sslconnectionsocketfactory.allow_al L_hostname_verifier);
Sslcontext Sslcontext = sslcontext.getinstance ("SSLv3"); Establish a certificate entity
javax.net.ssl.trustmanager[] Trustallcerts = new Javax.net.ssl.trustmanager[1];
Javax.net.ssl.TrustManager TM = new MiTM ();
TRUSTALLCERTS[0] = TM;
Sslcontext.init (NULL, trustallcerts, NULL);
Sslconnectionsocketfactory SSLSF = new Sslconnectionsocketfactory (Sslcontext,sslconnectionsocketfactory.allow_all_ Hostname_verifier);
Closeablehttpclient httpclient = Httpclients.custom (). Setsslsocketfactory (SSLSF). build ();


try{
String url = "https://127.0.0.1:8787/";


HttpPost HttpPost = new HttpPost (URL);
Gson Gson = new Gson ();
Entitybuilder Entitybuilder = Entitybuilder.create ();
Getlivelistreq req = new Getlivelistreq ();
Req.setmsgtype ("Getlivelist");
Req.setnum (1);
Req.setstart (1);
Req.setsessionid ("1001");
Loginreq req = new Loginreq ();
Req.setmsgtype ("Login");
Req.setusername ("11");
Entitybuilder.settext (Gson.tojson (req));
Httppost.setentity (Entitybuilder.build ());

SYSTEM.OUT.PRINTLN ("Executing request" + Httppost.geturi ());

responsehandler<string> ResponseHandler = new Responsehandler<string> () {
Public String handleresponse (final HttpResponse response) throws clientprotocolexception,ioexception{
int status = Response.getstatusline (). Getstatuscode ();
if (Status >= && status < 400) {
System.out.println ("Send success");
httpentity entity = response.getentity ();
Return entity!=null? Entityutils.tostring (entity): null;
}else{
throw new Clientprotocolexception ("Unexpected response status:" + status);
}
}
};
String responsebody = Httpclient.execute (Httppost,responsehandler);
System.out.println ("-------------------------------------------");
System.out.println (responsebody);
System.out.println ("-------------------------------------------");
}finally{
Httpclient.close ();
}
}


Static class MiTM implements Javax.net.ssl.TrustManager,
Javax.net.ssl.X509TrustManager {
Public java.security.cert.x509certificate[] Getacceptedissuers () {
return null;
}

public Boolean isservertrusted (
Java.security.cert.x509certificate[] certs) {
return true;
}

public Boolean isclienttrusted (
Java.security.cert.x509certificate[] certs) {
return true;
}

public void checkservertrusted (
Java.security.cert.x509certificate[] certs, String authtype)
Throws Java.security.cert.CertificateException {
Return
}

public void checkclienttrusted (
Java.security.cert.x509certificate[] certs, String authtype)
Throws Java.security.cert.CertificateException {
Return
}
}

}

Java HTTPS client trusts all certificates

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.