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