1. 程式人生 > >Java 傳送HTTP或HTTPS請求獲取網頁碼源(1)

Java 傳送HTTP或HTTPS請求獲取網頁碼源(1)

閒來無事,封裝了一個簡單的工具類,用於向某個URL傳送請求並獲取響應的文字內容。傳送HTTP請求的方式比較簡單,但用來發送HTTPS請求卻不行的。
於是查了一些資料,根據前人經驗寫了這個工具類,能夠區分URL是HTTP型別還是HTTPS型別的,但對HTTPS方式的原理的研究尚淺,也只是拿來便用。
因為是工具類,所有方法均為static修飾,但只對外部提供了getSourceFromUrl(String url,String charsetName) 和 getSourceFromUrl(String url) 
兩個過載的方法,第一個可以按指定編碼讀取流。
碼源如下,JDK1.7下測試通過,僅使用JDK提供的Jar包:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import
javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; /** * 通用工具類 * 更新:2016-12-3 16:13:27 * 版本:1.0.0 */ public final class CommonNetUtil { //預設編碼 private final static String CHARSETNAME = "UTF-8"
; //協議型別 private final static String HTTP = "http:"; private final static String HTTPS = "https:"; /** * 判斷協議型別,轉發到不同的方法進行處理 * @throws IOException */ private static String chooseProtocol(String url,String charsetName) throws IOException{ if (url == null) { throw new RuntimeException("url shouldn't be null."); } //字元編碼格式 if (charsetName == null){ throw new NullPointerException("charsetName shouldn't be null."); } if (!url.startsWith(HTTP) && !url.startsWith(HTTPS)) { throw new RuntimeException("url format is not supported."); } //如果是http協議 if (url.startsWith(HTTP)) { return getFromHttp(url,charsetName); }else if (url.startsWith(HTTPS)) { //如果是https協議 return getFromHttps(url, charsetName); } return null; } /** * http協議的url * @param url * @param charsetName * @return * @throws IOException */ private static String getFromHttp(String url,String charsetName) throws IOException{ HttpURLConnection con = null; try { // 開啟網頁 con = (HttpURLConnection) new URL(url).openConnection(); con.connect(); //判斷狀態碼 if (con.getResponseCode() == 200) { //讀取流 return getTextFromCon(con,charsetName); } } catch (IOException e) { throw new IOException("Connet exception.", e); } finally { if (con != null) { con.disconnect(); } } return null; } /** * https協議的url * @param url * @param charsetName * @return * @throws IOException */ private static String getFromHttps(String url,String charsetName) throws IOException{ HttpsURLConnection httpsConn = null; try { //構造TrustManager 物件陣列 TrustManager[] tm = {new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } }}; //建立SSLContext物件,並使用我們指定的信任管理器初始化 SSLContext sslContext = SSLContext.getInstance("SSL","SunJSSE"); sslContext.init(null, tm, new SecureRandom()); //從上述SSLContext物件中得到SSLSocketFactory物件 SSLSocketFactory ssf = sslContext.getSocketFactory(); //建立HttpsURLConnection物件,並設定其SSLSocketFactory物件 httpsConn = (HttpsURLConnection)new URL(url).openConnection(); httpsConn.setSSLSocketFactory(ssf); //連線 httpsConn.connect(); //判斷狀態碼 if (httpsConn.getResponseCode() == 200) { //讀取流 return getTextFromCon(httpsConn,charsetName); } } catch (Exception e) { throw new IOException("Connet exception.", e); }finally{ if (httpsConn != null) { httpsConn.disconnect(); } } return null; } /** * 從流中讀取資料 * @param con * @param charsetName * @return * @throws IOException */ private static String getTextFromCon(URLConnection con,String charsetName) throws IOException{ StringBuffer sb = new StringBuffer(); try( BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),charsetName)); ) { String s = null; while ((s = br.readLine()) != null) { //sb.append(s); //不換行 sb.append(s + "\n"); //換行 } return sb.toString(); } catch (IOException e) { throw new IOException("Read stream exception.", e); } } /** * 根據URL獲取碼源 * @param url * @return 返回字串型碼源 * @throws IOException */ public static String getSourceFromUrl(String url) throws IOException { return chooseProtocol(url, CHARSETNAME); } /** * 根據URL獲取碼源,可指定用何種編碼進行解碼 * @param url * @param charsetName 編碼型別 * @return 返回字串型碼源 * @throws IOException */ public static String getSourceFromUrl(String url,String charsetName) throws IOException { return chooseProtocol(url, charsetName); } }

測試類:

import java.io.IOException;
/* 測試類*/
public class Test {

    public static void main(String[] args) {

        String url = "https://www.baidu.com";
        try {
            String content= CommonNetUtil.getSourceFromUrl(url);
            System.out.println(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

結果:
效果圖

記錄到此。

傳送HTTPS請求參考了此文,http://blog.csdn.net/lifj07/article/details/8638098,表示感謝。