上一篇文章寫了爬蟲是怎麼回事,這篇寫怎麼請求URLhtml
經常使用的組件是HttpClient,官方地址:HttpClient官網java
我剛開始找了不少httpclient的例子,不過httpclient發展的太快,各類API亂飛,索性仍是去官網吧,靠譜apache
本文使用的是maven依賴app
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.3.2</version> </dependency>
我簡單寫一寫httpclient的使用方法步驟和小例子
maven
獲取httpclient實例,實例獲取是用靜態方法獲取的url
主要是使用HttpClients這個類的靜態方法進行獲取
code
獲取httpGet實例component
httpclient封裝了http的各類方法,get只是其中一個,還有POST,HEAD之類的均可以
htm
執行請求獲取httpResponse響應utf-8
執行就是httpclient實例execute httpget實例
解析response響應的內容
第三步返回的結果就是響應
解析響應呢,有兩種方法,一種是httpclient提供的httpentity實例解析的,還有一種是從輸入流inputstream中獲取,例子中是從inputstream中獲取
記得關閉資源
根據上面四點咱們寫出一下例子
package com.hldh.river; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created by liuhj on 2016/1/4. */ public class HttpGetUtils { /** * get 方法 * @param url * @return */ public static String get(String url){ String result = ""; try { //獲取httpclient實例 CloseableHttpClient httpclient = HttpClients.createDefault(); //獲取方法實例。GET HttpGet httpGet = new HttpGet(url); //執行方法獲得響應 CloseableHttpResponse response = httpclient.execute(httpGet); try { //若是正確執行並且返回值正確,便可解析 if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); //從輸入流中解析結果 result = readResponse(entity, "utf-8"); } } finally { httpclient.close(); response.close(); } }catch (Exception e){ e.printStackTrace(); } return result; } /** * stream讀取內容,能夠傳入字符格式 * @param resEntity * @param charset * @return */ private static String readResponse(HttpEntity resEntity, String charset) { StringBuffer res = new StringBuffer(); BufferedReader reader = null; try { if (resEntity == null) { return null; } reader = new BufferedReader(new InputStreamReader( resEntity.getContent(), charset)); String line = null; while ((line = reader.readLine()) != null) { res.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { } } return res.toString(); } }
本文旨在寫一寫使用到的API,把具體的請求過程記錄一下,不是API詳解,若是想了解具體的使用,我建議去官網查看,第一手資料才最有價值!!!
地址:https://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/index.html