Scenario 2
Import java.io.*; importJava.net.url;importJava.net.urlconnection;importJava.security.securerandom;importJava.security.cert.certificateexception;importJava.security.cert.x509certificate;import java.util.*; importJavax.net.ssl.hostnameverifier;importJavax.net.ssl.httpsurlconnection;importJavax.net.ssl.sslcontext;importJavax.net.ssl.sslsession;importJavax.net.ssl.trustmanager;importJavax.net.ssl.x509trustmanager;public classhttprequest{/** * Request to send a GET method to a specified URL * @param URL to send the request * @param the param request parameter, the request parameter should be in the form of name1=value1&name2=value2. * The response result of the remote resource represented by the @return URL */public staticString get (string url, string param) {return Get (URL, param, false); }/** * Send a request for a GET method to the specified URL * @param URL to send the request * @param param request parameter, the request parameter should be in the form of name1=value1&name2=value2. * The response result of the remote resource represented by the @return URL */public staticString Get (string url, string param, Boolean ishttpsrequest) {string result = ""; BufferedReader in = null; Try{if(ishttpsrequest) {httprequest.instance (). Trusthpps ();} String urlnamestring = URL + "?" +Param URL Realurl = newURL (urlnamestring); Connection between open and URL urlconnection connection =Realurl.openconnection (); Set common request Properties Connection.setrequestproperty ("Accept", "*/*"); Connection.setrequestproperty ("Connection", "keep-alive"); Connection.setrequestproperty ("User-agent", "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); Establish an actual connectionConnection.connect (); Get all response header fields Map<string, list<string>> Map =Connection.getheaderfields (); Traverse all the response header fields for(String Key:map.keySet ()) {System.out.println (key + "--->" +Map.get (key)); }//define BufferedReader input stream to read the response of the URL in = new BufferedReader (newInputStreamReader (Connection.getinputstream ())); String Line; while (line = In.readline ())! = NULL) {result + =Line }} catch(Exception e) {System.out.println ("Send GET request exception! " +e); E.printstacktrace (); }//Use finally block to close the input stream finally{Try{if (in! = NULL) {in.close ();}} Catch(Exception E2) {E2.printstacktrace ();} } returnResult /** * Request to send a POST method to the specified URL * @param URL to send the request * @param param request parameter, the request parameter should be in the form of name1=value1&name2=value2. * @return response results for remote resources */public staticString post (string url, string param) {return Post (URL, param, false); /** * Request to send a POST method to the specified URL * @param URL to send the request * @param param request parameter, the request parameter should be in the form of name1=value1&name2=value2. * @param ishttpsrequest whether the HTTPS request * @return The response result of the remote resource represented by */public staticString Post (string url, string param, Boolean ishttpsrequest) {PrintWriter out = null; BufferedReader in = null; String result = ""; Try{if(ishttpsrequest) {httprequest.instance (). Trusthpps ();} URL Realurl = newURL (URL); Connection between open and URL URLConnection conn =Realurl.openconnection (); Set common request Properties Conn.setrequestproperty ("Accept", "*/*"); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("User-agent", "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); To send a POST request, you must set the following two lines Conn.setdooutput (true); Conn.setdoinput (True); Gets the output stream corresponding to the URLConnection object out = newPrintWriter (Conn.getoutputstream ()); Send Request parametersOut.print (param); Buffer for flush output streamOut.flush (); Defines the response of the BufferedReader input stream to read the URL in = newBufferedReader (NewInputStreamReader (Conn.getinputstream ())); String Line; while (line = In.readline ())! = NULL) {result + =Line }} catch(Exception e) {System.out.println ("send POST request exception! " +e); E.printstacktrace (); }//Use the finally block to close the output stream, the input stream finally{Try{if (out! = NULL) {out.close ();} if (In! = NULL) {in.close ();}} Catch(IOException ex) {Ex.printstacktrace ();} } returnResult }/** * Trust certificate Management * */private static trustmanager[] Trustallcerts = new trustmanager[] {new X509trustmanager () {@Override public void Che Ckclienttrusted (x509certificate[] chain, String authtype) throws Certificateexception {//TODO auto-generated method Stu b} @Override public void checkservertrusted (x509certificate[] chain, String authtype) throws Certificateexception {//To Do auto-generated method stubs} @Override public x509certificate[] Getacceptedissuers () {//TODO auto-generated Method St UB return NULL; } } };/** * Host Certificate Certification * */Private class Nullhostnameverifier implements Hostnameverifier {/* * (non-javadoc) * @see Javax.net.ssl.HostnameVeri Fier#verify (java.lang.String, * javax.net.ssl.SSLSession) */@Override public boolean verify (String arg0, SSLsession ARG1) {//TODO auto-generated method stub return true;}}private staticHttpRequest _httprequest;/** * HTTP Request Encapsulation Instance * */public static HttpRequest Instance () {if (_httprequest = = Null
HttpRequest ();} return
_httprequest;}/** * Trust HTTPS * * /public void Trusthpps () t Hrows Exception {httpsurlconnection. Setdefaulthostnameverifier (New Nullhostnameverifier ()); Sslcontext sc = sslcontext.getinstance ("TLS"); Sc.init (NULL, httprequest.trustallcerts, new securerandom ()); Httpsurlconnection.setdefaultsslsocketfactory (Sc.getsocketfactory ()); }}
Test code
String address= "https web address"; Bypass HTTPS certificate Scheme 2 String reslt2= httprequest.get (address,null,true); System.out.println (RESLT2); Indicates that no exception occurred during the request and the webpage returned normally, which means that the HTTPS certificate was successfully bypassed; The magenta part code is the key code
Java.httpclient Bypass HTTPS certificate solution two