When httpclient accesses https, peer can't appears.

Source: Internet
Author: User

Package util;

Import java. Security. cert. certificateexception;

Import javax.net. SSL. sslcontext;
Import javax.net. SSL. trustmanager;
Import javax.net. SSL. x509trustmanager;
Import javax. Security. cert. x509certificate;

Import org. Apache. http. Client. httpclient;
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;
Import org. Apache. http. impl. Conn. tsccm. threadsafeclientconnmanager;

Public class webclientdevwrapper {

Public static httpclient wrapclient (httpclient base ){
Try {
Sslcontext CTX = sslcontext. getinstance ("TLS ");
X509trustmanager TM = new x509trustmanager (){
Public java. Security. cert. x509certificate [] getacceptedissuers (){
Return NULL;
}
Public void checkclienttrusted (x509certificate [] arg0,
String arg1) throws certificateexception {

}
Public void checkservertrusted (x509certificate [] arg0,
String arg1) throws certificateexception {

}
@ Override
Public void checkclienttrusted (
Java. Security. cert. x509certificate [] chain,
String authtype) throws certificateexception {
// Todo auto-generated method stub

}
@ Override
Public void checkservertrusted (
Java. Security. cert. x509certificate [] chain,
String authtype) throws certificateexception {
// Todo auto-generated method stub

}
};
CTX. INIT (null, new trustmanager [] {TM}, null );
Sslsocketfactory SSF = new sslsocketfactory (CTX, sslsocketfactory. allow_all_hostname_verifier );
Schemeregistry registry = new schemeregistry ();
Registry. Register (New Scheme ("HTTPS", 443, SSF ));
Threadsafeclientconnmanager Mgr = new threadsafeclientconnmanager (Registry );
Return new defaulthttpclient (MGR, base. getparams ());
} Catch (exception ex ){
Ex. printstacktrace ();
Return NULL;
}
}

Public static httpclient getclient (httpclient client ){
Return wrapclient (client );
}

}

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.