Java.httpclient Bypass HTTPS certificate solution one

Source: Internet
Author: User

Scenario 1

ImportJavax.net.ssl.*;ImportJava.io.*;ImportJava.net.URL;Importjava.security.KeyManagementException;Importjava.security.NoSuchAlgorithmException;ImportJava.security.SecureRandom;Importjava.security.cert.CertificateException;Importjava.security.cert.X509Certificate;/*** HTTPS helper*/ Public classHttpsutil {private static Final class Defaulttrustmanager implements X509trustmanager {@Override public void check Clienttrusted (x509certificate[] chain, String authtype) throws certificateexception {} @Override Pub LIC void checkservertrusted (x509certificate[] chain, String authtype) throws certificateexception {} @Overr        IDE public x509certificate[] Getacceptedissuers () {return null; }    }    Private StaticHttpsurlconnection gethttpsurlconnection (String uri, String method)throwsIOException { sslcontext ctx = null;            try {ctx = sslcontext.getinstance ("TLS");        Ctx.init (New keymanager[0], new trustmanager[] {new Defaulttrustmanager ()}, New SecureRandom ());        } catch (Keymanagementexception e) {e.printstacktrace (); } catch (nosuchalgorithmexception e) {e.printstacktrace (); }  sslsocketfactory SSF        = ctx.getsocketfactory (); URL URL=NewURL (URI); Httpsurlconnection Httpsconn=(httpsurlconnection) url.openconnection (); //bypass HTTPS-related certificate key code-start httpsconn.setsslsocketfactory (SSF); //bypass HTTPS-related certificate critical code-endHttpsconn.sethostnameverifier (NewHostnameverifier () {@Override Public BooleanVerify (String arg0, sslsession arg1) {return true;        }        });        Httpsconn.setrequestmethod (method); Httpsconn.setdoinput (true); Httpsconn.setdooutput (true); returnHttpsconn; }    Private Static byte[] Getbytesfromstream (InputStream is)throwsIOException {bytearrayoutputstream BAOs=NewBytearrayoutputstream (); byte[] KB =New byte[1024]; intLen;  while(len = Is.read (KB))! =-1) {Baos.write (KB,0, Len); }        byte[] bytes =Baos.tobytearray ();        Baos.close ();        Is.close (); returnbytes; }    Private Static voidSetbytestostream (OutputStream OS,byte[] bytes)throwsIOException {bytearrayinputstream Bais=Newbytearrayinputstream (bytes); byte[] KB =New byte[1024]; intLen;  while(len = Bais.read (KB))! =-1) {Os.write (KB,0, Len);        } os.flush ();        Os.close ();    Bais.close (); }     Public Static byte[] doget (String URI)throwsIOException {httpsurlconnection httpsconn= Gethttpsurlconnection (URI, "GET"); returnGetbytesfromstream (Httpsconn.getinputstream ()); }     Public Static byte[] DoPost (String uri, string data)throwsIOException {httpsurlconnection httpsconn= Gethttpsurlconnection (URI, "POST");        Setbytestostream (Httpsconn.getoutputstream (), data.getbytes ()); returnGetbytesfromstream (Httpsconn.getinputstream ()); }}

Test code

         String address = "HTTPS Web addresses";         // Bypass HTTPS certificate scheme 1        byte [] resultbytes=httpsutil.doget (address);        String result=new  string (resultbytes);        SYSTEM.OUT.PRINTLN (result);             // as long as no exception occurs during the request, the problem of successfully bypassing the HTTPS certificate is explained, and the Red text section is the key in the example above

Java.httpclient Bypass HTTPS certificate solution one

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.