網絡爬蟲(一)- 基本使用

get請求

    get請求的基本使用

    // 1. 打開瀏覽器,建立HttpClient對象    
    CloseableHttpClient httpClient = HttpClients.createDefault();
    
    // 2.輸入網址,發起get請求建立HttpGet對象
    HttpGet get = new HttpGet("http://112.124.1.187/index.html?typeId=16");
    
    // 3.發情請求,返回響應,使用HttpClient對象發起請求
    CloseableHttpResponse response = httpClient.execute(get);
    
    // 4.解析響應,獲取數據
    if(response.getStatusLine().getStatusCode() == 200){
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity,"utf-8");
        System.out.println(content);
    }

 

    get請求帶參數(能夠直接寫在地址後,可是構成硬編碼)

    // 1. 打開瀏覽器,建立HttpClient對象
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        // 地址:http://112.124.1.187/index.html?typeId=16.帶有參數
        // 建立URIBuilder
        URIBuilder uriBuilder = new URIBuilder("http://112.124.1.187/index.html");
        // 添加參數
        // 多個參數能夠連着添加,在後面連着setParameter(key,value)
        uriBuilder.setParameter("typeId","16");
        // 2.輸入網址,發起get請求建立HttpGet對象
        HttpGet get = new HttpGet(uriBuilder.build());
        // 3.發情請求,返回響應,使用HttpClient對象發起請求
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(get);
            // 4.解析響應,獲取數據
            if(response.getStatusLine().getStatusCode() == 200){
                HttpEntity entity = response.getEntity();
                String content = EntityUtils.toString(entity,"utf-8");
                System.out.println(content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

 

post請求

    基本使用與get相同把HttpGet改成HttpPost就能夠了。html

    post請求帶參數

    // 1. 打開瀏覽器,建立HttpClient對象
    CloseableHttpClient httpClient = HttpClients.createDefault();
    
    // 地址:http://112.124.1.187/index.html?typeId=16.帶有參數
    // 2.輸入網址,發起post請求建立HttpPost對象
    HttpPost post = new HttpPost("http://112.124.1.187/index.html");
    // 2.1 聲明List集合,封裝表單中的參數
    List<NameValuePair> params = new ArrayList<>();
    // 2.2 添加參數
    params.add(new BasicNameValuePair("typeId","16"));
    // 2.3 建立表單的Entity對象,對參數進行url編碼
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params,"utf-8");
    // 2.4 設置表單的Entity對象到Post請求中
    post.setEntity(formEntity);
    
    // 3.發情請求,返回響應,使用HttpClient對象發起請求
    CloseableHttpResponse response = null;
    try {
        response = httpClient.execute(post);
        // 4.解析響應,獲取數據
        if(response.getStatusLine().getStatusCode() == 200){
            HttpEntity entity = response.getEntity();
            String content = EntityUtils.toString(entity,"utf-8");
            System.out.println(content);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        if(response != null){
            response.close();
        }
        httpClient.close();
    }

 

像每個鏈接操做同樣,HttpClent 鏈接一次,再斷開,再要用時,繼續鏈接,再斷開。構成浪費資源現象。須要用到 "池" 這個概念。瀏覽器

HttpClient-鏈接池

    public static void     main(String[] args) {
        // 建立鏈接池管理器
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        // 設置最大鏈接數
        cm.setMaxTotal(10);
        // 設置每一個主機最大鏈接數
        cm.setDefaultMaxPerRoute(2);
    
        // 使用鏈接池管理器發起請求
        doGet(cm);
        doGet(cm);
    }
    
    private static void doGet(PoolingHttpClientConnectionManager cm) {
        // 從鏈接池中獲取HttpClient對象
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
    
        HttpGet httpGet = new HttpGet("http://112.124.1.187");
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode() == 200){
                String content = EntityUtils.toString(response.getEntity(),"utf-8");
                System.out.println(content.length());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 不用關閉HttpClient,交由池來管理
            // httpClient.close();
        }
    }

 

請求參數

    這個請求參數不是放在url地址後面的參數,而是你在請求過程當中,所涉及到須要事先定好的規則。好比,在請求過程當中,有時候由於網絡緣由,或目標服務器的緣由,請求須要更長的時間才能完成,就須要咱們自定義相關的時間。服務器

    HttpGet get =     new HttpGet("http://112.124.1.187/index.html?typeId=16");
    // 配置請求信息
    RequestConfig config = RequestConfig.custom().setConnectTimeout(10000)  // 建立鏈接的最長時間,單位是毫秒
                            .setConnectionRequestTimeout(500)   // 設置獲取鏈接的最長時間,單位是毫秒
                            .setSocketTimeout(10 * 1000)    // 設置數據傳輸的最長時間,單位是毫秒
                            .build();
    // 將配置給請求
    get.setConfig(config);
相關文章
相關標籤/搜索