Java简单爬虫系列(2)---HttpClient的使用

上一篇文章写了爬虫是怎么回事,这篇写怎么请求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

  1. 获取httpclient实例,实例获取是用静态方法获取的url

    1. 主要是使用HttpClients这个类的静态方法进行获取
      code

  2. 获取httpGet实例component

    1. httpclient封装了http的各类方法,get只是其中一个,还有POST,HEAD之类的均可以
      htm

  3. 执行请求获取httpResponse响应utf-8

    1. 执行就是httpclient实例execute httpget实例

  4. 解析response响应的内容

    1. 第三步返回的结果就是响应

    2. 解析响应呢,有两种方法,一种是httpclient提供的httpentity实例解析的,还有一种是从输入流inputstream中获取,例子中是从inputstream中获取

  5. 记得关闭资源


根据上面四点咱们写出一下例子

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

相关文章
相关标签/搜索