Ignoring SSL certificate validation when calling an interface through HttpClient

Source: Internet
Author: User
Tags stub ssl certificate

When the interface address of the call is found to be in HTTPS form during the project, the exception is thrown by the httpclient call: Exception in thread "main" javax.net.ssl.SSLPeerUnverifiedException: Peer not authenticated, find data discovery because HTTPS uses SSL digital certificate authentication, and the server does not provide the corresponding SSL digital certificate, which requires the call to bypass the checksum, the implementation of the code is as follows:

1.httpclient

public static void Main (string[] args) throws HttpException, IOException {
HttpClient httpclient = new Defaulthttpclient ();
HttpClient = Wrapclient (httpclient);
HttpPost HttpPost = new HttpPost (URL);

Stringentity myentity = new stringentity (Test (), "utf-8");

Httppost.addheader ("Content-type", "text/html;charset=utf-8");

Httppost.setentity (myentity);
Httppost.addheader ("Authorization", "bearer Ywmtlzkthgg9eesdfpg0cx2i0waaaucagn61pdzr1lx5o8fyo0h8cwinv1nfexi");
HttpResponse response = Httpclient.execute (HttpPost);
Httpentity resentity = response.getentity ();
InputStreamReader reader = new InputStreamReader (Resentity.getcontent (), "utf-8");
char[] buff = new char[1024];
int length = 0;
while (length = Reader.read (Buff))!=-1) {
System.out.println (New String (buff, 0, length));
}
Httpclient.getconnectionmanager (). Shutdown ();
}

2. Canceling the verification of SSL

public static httpclient wrapclient (httpclient base) {
try {
Sslcontext CTX = sslcontext.getinstance ("TLS");
X509trustmanager TM = new X509trustmanager () {
public void checkclienttrusted (x509certificate[] Xcs,
String string) {
}


public void checkservertrusted (x509certificate[] Xcs,
String string) {
}


Public x509certificate[] Getacceptedissuers () {
return null;
}
};

x509hostnameverifier HV = new X509hostnameverifier () {


@Override
public boolean verify (String hostname, sslsession session) {
TODO auto-generated Method Stub
return true;
}


@Override
public void Verify (String arg0, Sslsocket arg1) throws IOException {
TODO auto-generated Method Stub

}


@Override
public void Verify (String arg0, X509Certificate arg1)
Throws Sslexception {
TODO auto-generated Method Stub

}


@Override
public void Verify (String arg0, string[] arg1, string[] arg2)
Throws Sslexception {
TODO auto-generated Method Stub

}

};
Ctx.init (NULL, new trustmanager[] {TM}, NULL);
Sslsocketfactory SSF = new Sslsocketfactory (CTX, (x509hostnameverifier) HV);

Clientconnectionmanager CCM = Base.getconnectionmanager ();
schemeregistry sr = Ccm.getschemeregistry ();
Sr.register (New Scheme ("https", 443, SSF));
return new Defaulthttpclient (CCM, Base.getparams ());
catch (Exception ex) {
Ex.printstacktrace ();
return null;
}
}

3. Required jar package for httpclient involved Jar pack

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.