爬蟲學習3-網頁內容獲取工具URLConnection
阿新 • • 發佈:2018-12-22
URLConnection也是java後臺用來獲取請求資料類,demo程式碼如下:
package com.jack.spiderone.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.*; /** * create by jack 2018/11/17 * * @author jack * @date: 2018/11/17 22:02 * @Description: * * URLConnection 是 JDK 自帶的一個抽象類,其代表應用程式和 URL 之間的通訊連結。 * 在網路爬蟲中,我們可以使用 URLConnection 請求一個 URL 地址, * 然後獲取流資訊,通過對流資訊的操作,可獲得請求到的實體內容 * */ public class URLConnectionTest { /** * 1,URLConnection 的使用 */ public static void test1() throws IOException { //使用 URLConnection 時,我們無法直接例項化物件, // 但可以通過在 URL 上呼叫 openConnection() 方法建立一個連線物件 URL url = new URL("http://www.w3school.com.cn/b.asp"); URLConnection conn = url.openConnection(); } /** * 2,獲取資料內容 * @throws IOException */ public static void test2() throws IOException { URL url = new URL("http://www.w3school.com.cn/b.asp"); URLConnection conn = url.openConnection(); InputStream in=conn.getInputStream(); // 定義BufferedReader輸入流來讀取URL的響應 BufferedReader reader = new BufferedReader( new InputStreamReader(in)); String line; //這裡的內容是html String html = ""; while ((line = reader.readLine()) != null) { html += line; } System.out.println(html); } /** * 3,GET請求 * @throws IOException */ public static void test3() throws IOException{ //初始化URL URL url = new URL("http://www.w3school.com.cn/b.asp"); //使用URLConnection的子類HttpURLConnection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 允許Input //將此 URLConnection 的 doInput 欄位的值設定為true,表示 URL 連線可用於輸入 conn.setDoInput(true); //設定請求的方法GET conn.setRequestMethod("GET"); //conn.setRequestMethod("POST"); //注意該網頁只能使用GET請求 //建立實際連線操作,子類 HttpURLConnection 建立實際連線需要使用 connect() 方法 conn.connect(); //獲取響應狀態碼 int statusCode = conn.getResponseCode(); String responseBody = null; //如果響應狀態碼為200 if (HttpURLConnection.HTTP_OK == statusCode ) { // 定義BufferedReader輸入流來讀取URL的響應 ,這裡設定編碼 BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(conn.getInputStream(), "GBK")); //讀取內容 String readLine = null; StringBuffer response = new StringBuffer(); while (null != (readLine = bufferedReader.readLine())) { response.append(readLine); } bufferedReader.close(); responseBody = response.toString(); } System.out.println(responseBody); } /** * 4,POST 請求 * * @throws IOException */ public static void test4() throws IOException{ //Post表單需要提交的引數 String wen = "EH629625211CS"; String action = "ajax"; //初始化URL URL url = new URL("http://www.kd185.com/ems.php"); //使用URLConnection的子類HttpURLConnection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // //允許Input、Output,不使用Cache //將此 URLConnection 的 doOutput 欄位的值設定為true,設定 setDoOutput() 為 True。該程式能夠成功獲取伺服器返回的資料 conn.setDoOutput(true); //將此 URLConnection 的 doInput 欄位的值設定為true conn.setDoInput(true); // 將此 URLConnection 的 useCaches 欄位的值設定為false conn.setUseCaches(false); //Post提交引數 conn.setRequestMethod("POST"); StringBuffer params = new StringBuffer(); // 表單引數拼接 params.append("wen").append("=").append(wen).append("&") .append("action").append("=").append(action); byte[] bypes = params.toString().getBytes(); // 在連線中新增引數 conn.getOutputStream().write(bypes); // 定義BufferedReader輸入流來讀取URL的響應 ,這裡設定編碼 BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(conn.getInputStream(), "utf-8")); String line; String html = ""; while ((line = bufferedReader.readLine()) != null) { html += line; } System.out.println(html); } /** * * 5,請求頭的設定 *在 URLConnection 以及 HttpURLConnection 中, * 我們可以使 setRequestProperty(key,value) 的形式設定請求頭資訊 * * @throws IOException */ public static void test5() throws IOException{ //初始化URL URL url = new URL("http://www.w3school.com.cn/b.asp"); //使用URLConnection URLConnection conn = url.openConnection(); //新增請求頭資訊 conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"); conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9"); conn.setRequestProperty("Host", "www.w3school.com.cn"); conn.setRequestProperty("Cache-Control", "max-age=0"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"); //實際連線操作 conn.connect(); } /** * 6,超時設定 *超時設定,防止網路異常時,可能會導致程式僵死而不繼續往下執行的情況 * @throws IOException */ public static void test6() throws IOException{ //初始化URL URL url = new URL("http://www.w3school.com.cn/b.asp"); URLConnection conn = url.openConnection(); // HttpURLConnection conn = (HttpURLConnection) url.openConnection() //連線超時 單位毫秒 conn.setConnectTimeout(30000); //讀取超時 單位毫秒 conn.setReadTimeout(30000); } /** * 7,代理的設定 *在 URLConnection 以及 HttpURLConnection 中,我們可以使用 Proxy 進行設定代理 * @throws IOException */ public static void test7() throws IOException{ //設定代理 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("171.97.67.160", 3128)); URL url = new URL("http://www.w3school.com.cn/b.asp"); //以代理的方式建立連線 URLConnection conn = url.openConnection(proxy); //建立實體連線 conn.connect(); // 定義BufferedReader輸入流來讀取URL的響應 ,這裡設定編碼 BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(conn.getInputStream(), "gbk")); String line; String html = ""; while ((line = bufferedReader.readLine()) != null) { html += line; } System.out.println(html); } public static void test8() throws IOException{ } public static void main(String[] args) throws IOException { test4(); } }
原始碼地址: