HttpClient HTTPS ignores certificate direct request

Source: Internet
Author: User

HttpClient request HTTPS to ignore certificate direct request:

Encapsulating HTTPS ignores certificate codes

Package com.sunrise.web.utils;
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; public class Certificateauthorityhttpclientutil extends defaulthttpclient{certificateauthorityhttpclientutil ()
    Throws exception{super ();
    Sslcontext CTX = sslcontext.getinstance ("TLS");  X509trustmanager TM = new X509trustmanager () {@Override public void checkclienttrusted (x509certificate[) Chain, String authtype) throws certificateexception {} @Override public void checkservertrusted ( X509certificate[] chain, String authtype) throws CertificateexceptiOn {} @Override public x509certificate[] 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)); }
   }

Call Interface:

	HttpClient httpclient = new Certificateauthorityhttpclientutil ();
	String Headofficepath = Properties.getproperty ("Headofficepath");


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.