1. 程式人生 > >Java基於httpclient獲取網頁資料,實現簡單網路爬蟲

Java基於httpclient獲取網頁資料,實現簡單網路爬蟲

1、pom檔案引入httpclient依賴

    <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>    

2、直接上程式碼

public static void getInternetData(String url) {
        logger.info("目標網路地址:url="+url);
        //初始化httpclient
        CloseableHttpClient client = HttpClients.createDefault();
        //get方法請求
        HttpGet getMethod = new HttpGet(url);
        //post方法請求
        HttpPost postMethod = new HttpPost(url);
        try {
            //執行響應 ,初始化response    
            CloseableHttpResponse response = client.execute(getMethod);
            //獲取響應狀態碼
            int statusCode = response.getStatusLine().getStatusCode();            
            logger.info("訪問響應狀態碼,statusCode="+statusCode);
            //獲取實體內容
            String entity = EntityUtils.toString(response.getEntity(),"utf-8");
            logger.info("訪問網路響應資訊:response="+entity);
            //消耗實體:關閉HttpEntity的流實體
            EntityUtils.consume(response.getEntity());
            response.close();
            client.close();
        } catch (Exception e) {
            logger.info("獲取網路資料異常",e);
        }
    }  

    public static void main(String[] args) {
        getInternetData("https://cn.bing.com/");
    }