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