Java Crawler--https Bypass certificate

Source: Internet
Author: User

The HTTPS Web server is a certificate. is issued by the website's own server and is not widely accepted by the browser or operating system.

when using Closeablehttpclient often encounter certificate errors (this is how the website is known)

Now requires SSL bypass certificate, the following directly paste the code, the call only need to send the request after the new Httpsberber (the file's byte code);

1 ImportJavax.net.ssl.HostnameVerifier;2 Importjavax.net.ssl.HttpsURLConnection;3 Importjavax.net.ssl.SSLSession;4 ImportJava.util.logging.Logger;5 6  Public classHttpsberber {7      PublicHttpsberber (String name)throwsException {8Logger =Logger.getlogger (name);9 trustallhttpscertificates ();Tenhostnameverifier HV =NewHostnameverifier () { One              Public BooleanVerify (String urlhostname, sslsession session) { ALogger.info ("Warning:url Host:" + urlhostname + "vs." +session.getpeerhost ()); -                 return true; -             } the         }; - httpsurlconnection.setdefaulthostnameverifier (HV); -     } -     Private StaticLogger Logger; +     Private Static voidTrustallhttpscertificates ()throwsException { -javax.net.ssl.trustmanager[] Trustallcerts =NewJavax.net.ssl.trustmanager[1]; +Javax.net.ssl.TrustManager TM =NewMiTM (); ATrustallcerts[0] =TM; atJavax.net.ssl.SSLContext sc =Javax.net.ssl.SSLContext -. getinstance ("SSL"); -Sc.init (NULL, Trustallcerts,NULL); - Javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory (SC - . Getsocketfactory ()); -     } in     Static classMiTMImplementsJavax.net.ssl.TrustManager, - Javax.net.ssl.X509TrustManager { to          Publicjava.security.cert.x509certificate[] Getacceptedissuers () { +             return NULL; -         } the  *          Public Booleanisservertrusted ( $ java.security.cert.x509certificate[] certs) {Panax Notoginseng             return true; -         } the  +          Public Booleanisclienttrusted ( A java.security.cert.x509certificate[] certs) { the             return true; +         } -  $          Public voidcheckservertrusted ( $ java.security.cert.x509certificate[] certs, String authtype) -                 throwsjava.security.cert.CertificateException { -             return; the         } - Wuyi          Public voidcheckclienttrusted ( the java.security.cert.x509certificate[] certs, String authtype) -                 throwsjava.security.cert.CertificateException { Wu             return; -         } About     } $}

Java Crawler--https Bypass certificate

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.