파충류가 서버를 빙빙 돌려서 검사하다

6113 단어 파충류
HostnameVerifier hv = new HostnameVerifier() {  
    public boolean verify(String urlHostName, SSLSession session) 
    {  
        System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());  
        return true;  
    }  
}; 
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
//  html 
        try {
            URL url = new URL(string);
            URLConnection connection = url.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
             connection.connect();
            /*URLConnection conn = url.openConnection();
            conn.setRequestProperty("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");*/
            isr=new InputStreamReader(connection.getInputStream(),Charset.forName("UTF-8")); 
            //isr=new InputStreamReader(conn.getInputStream(), "UTF-8");
            bufr = new BufferedReader(isr);
        } catch (Exception e) {
            e.printStackTrace();
        }

trustAllHttpsCertificates():
private void trustAllHttpsCertificates() {
        javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];  
        javax.net.ssl.TrustManager tm = new miTM();  
        trustAllCerts[0] = tm;  
        javax.net.ssl.SSLContext sc = null;
        try {
            sc = javax.net.ssl.SSLContext  
                    .getInstance("SSL");
        } catch (NoSuchAlgorithmException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }  
        try {
            sc.init(null, trustAllCerts, null);
        } catch (KeyManagementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc  
                .getSocketFactory());

    }
     static class miTM implements javax.net.ssl.TrustManager,  
     javax.net.ssl.X509TrustManager {  
 public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
     return null;  
 }  

 public boolean isServerTrusted(  
         java.security.cert.X509Certificate[] certs) {  
     return true;  
 }  

 public boolean isClientTrusted(  
         java.security.cert.X509Certificate[] certs) {  
     return true;  
 }  

 public void checkServerTrusted(  
         java.security.cert.X509Certificate[] certs, String authType)  
         throws java.security.cert.CertificateException {  
     return;  
 }  

 public void checkClientTrusted(  
         java.security.cert.X509Certificate[] certs, String authType)  
         throws java.security.cert.CertificateException {  
     return;  
 }  
}

좋은 웹페이지 즐겨찾기