欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HttpClient简介与实现

程序员文章站 2022-05-29 22:49:40
...
1. HttpClient简介

HttpClient官网:http://hc.apache.org/

HttpClient4.5:http://hc.apache.org/httpcomponents-client-4.5.x/

HttpClient官方文档:http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/index.html

HttpClient是Apache Jakarta Common下的子项目,可以用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。

HTTP协议可能是现在Internet上使用得最多、最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源。虽然在JDK的javanet包中已经提供了访问HTTP协议的基本功能,但是对于大部分应用程序来说,JDK库本身提供的功能还不够丰富和灵活。HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。爬虫主要是用HttpClient模拟浏览器请求第三方站点url,然后响应,获取网页数据,然后用Jsoup来提取我们需要的信息。


2. HttpClient实现HelloWorld

New -> Project -> Maven Project -> Next ->
Create a simple project -> Next ->
    Group Id: com.andrew
    Artifact Id: HttpClientDemo
    Version: 0.0.1-SNAPSHOT
    Packaging: jar
    
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.andrew</groupId>
  <artifactId>HttpClientDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
      <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
  </dependencies>
</project>

HelloWorld01.java

package com.andrew.httpClient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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 org.apache.http.util.EntityUtils;

public class HelloWorld01 {
    public static void main(String[] args) {
        CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建httpClient实例
        HttpGet httpGet = new HttpGet("http://www.csdn.net/"); // 创建httpget实例
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet); // 执行http get请求
        } catch (ClientProtocolException e) { // http协议异常
            e.printStackTrace();
        } catch (IOException e) { // io异常
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity(); // 获取返回实体
        try {
            System.out.println("网页内容:" + EntityUtils.toString(entity, "utf-8")); // 获取网页内容
        } catch (ParseException e) { // 解析异常
            e.printStackTrace();
        } catch (IOException e) { // io异常
            e.printStackTrace();
        }
        try {
            response.close(); // response关闭
        } catch (IOException e) { // io异常
            e.printStackTrace();
        }
        try {
            httpClient.close(); // httpClient关闭
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

HelloWorld02.java

package com.andrew.httpClient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
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 org.apache.http.util.EntityUtils;

public class HelloWorld02 {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建httpClient实例
        HttpGet httpGet = new HttpGet("http://www.csdn.net/"); // 创建httpget实例
        CloseableHttpResponse response = httpClient.execute(httpGet); // 执行http get请求
        HttpEntity entity = response.getEntity(); // 获取返回实体
        System.out.println("网页内容:" + EntityUtils.toString(entity, "utf-8")); // 获取网页内容
        response.close(); // response关闭
        httpClient.close(); // httpClient关闭
    }
}
相关标签: HttpClient