java抓取Https協議url地址的源碼的方法

閱讀本文以前,請先閱讀下文,經過下文將獲取到keystore文件:java

http://www.oschina.net/code/snippet_273576_18919web

在此,感謝一下stevenliu ,不過通過個人測試,發現stevenliu 文章中的源碼並無抓取到頁面源碼。多是我項目裏面的HttpClient版本較高的緣由。下面這個方法沒有問題:apache

package org.phoenix.cases.webservice;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.io.IOUtils;
/**
 * java經過加載認證證書,抓取https的url源碼方法
 * @author mengfeiyang
 *
 */
public class MyX509TrustManager implements X509TrustManager {
    private X509TrustManager sunJSSEX509TrustManager;
   
    public MyX509TrustManager(String keystoreFile,String pass) throws Exception {
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(new FileInputStream(keystoreFile),pass.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");
        tmf.init(ks);
        TrustManager tms [] = tmf.getTrustManagers();
        for (int i = 0; i < tms.length; i++) {
            if (tms[i] instanceof X509TrustManager) {
                sunJSSEX509TrustManager = (X509TrustManager) tms[i];
                return;
            }
        }
        throw new Exception("Couldn't initialize");
    }
    
    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        try {
            sunJSSEX509TrustManager.checkClientTrusted(chain, authType);
        } catch (CertificateException excep) {
         excep.printStackTrace();
        }
    }
    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType) {
        try {
            sunJSSEX509TrustManager.checkServerTrusted(chain, authType);
        } catch (CertificateException excep) {
         excep.printStackTrace();
        }
    }
    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return sunJSSEX509TrustManager.getAcceptedIssuers();
    }
    
    public static void main(String[] args) throws Exception {
        TrustManager[] tm = { new MyX509TrustManager("E:\\mycert.keystore","123456") };
        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
        sslContext.init(null, tm, new SecureRandom());
        SSLSocketFactory ssf = sslContext.getSocketFactory();
        URL myURL = new URL("https://beta.tf.360.cn/search/mapi?keyword=途牛");
        HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
        httpsConn.setSSLSocketFactory(ssf);
        InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());
        List<String> ls = IOUtils.readLines(insr);
        
        for(String l : ls){
         System.out.println(l);//打印源碼
        }
        
        httpsConn.getResponseCode();//獲取的狀態碼
 }
}

在個人phoenixframework自動化平臺的接口測試模塊中,下個版本將會增長https的操做。由於發現如今愈來愈多的網站是https了。phoenixframework平臺網站:http://www.cewan.laapi

相關文章
相關標籤/搜索