org.apache.http.client.HttpClient使用方法

官网说明:

http://hc.apache.org/httpclient-3.x/

Commons HttpClient项目现已结束,不再开发。它已被其HttpClient和HttpCore模块中的Apache HttpComponents项目所取代,它们提供更好的性能和更大的灵活性。

从2011年开始,org.apache.commons.httpclient就不再开发。这就是说,它已经落伍了。

方法的对称性上的区别

一、org.apache.http.client

org.apache.http.client在发起请求前,假如对某个参数a 进行url encode编码。服务端必须进行url decode。

//客户端编码

Stirng a=URLEncoder.encode(cont,"GBK");

//服务端解码

URLDecoder.decode(a,"gbk");

且服务器端获取到的参数a为可识别的没有任何变动的url encode后原值。

二、org.apache.commons.httpclient

org.apache.commons.httpclient则与之相反。

服务端获取到的a为不可识别的乱码,且不能用url decode解码。

//服务端解码

new String(cont.getBytes("ISO8859_1"), "GBK")

与时俱进

org.apache.http.client更好的性能和更大的灵活性。

可以很方便的支持json,xml等数据的传输。且http://mvnrepository.com上在不断的升级。超时、最大连接数等配置灵活方便。

三.pom.xml

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.11.3</version>
</dependency>

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.1</version>
        </dependency>

最最简单的方法

利用Jsoup直接获取HTML页面

Document doc = Jsoup.connect("http://www.xbiquge.la/xiaoshuodaquan/").get();
        Elements elements = doc.getElementsContainingOwnText("斗破苍穹");

四.简单使用方法

package com.feilong.reptile.util;

import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.Charsets;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;

public class MHttpClient {
    public void get(String url) throws Exception {
        // 创建HttpClient实例
        HttpClient client =  HttpClientBuilder.create().build();
        // 根据URL创建HttpGet实例
        HttpGet get = new HttpGet(url);
        // 执行get请求,得到返回体
        HttpResponse response = client.execute(get);
        // 判断是否正常返回
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            // 解析数据
            String data = EntityUtils.toString(response.getEntity(),Charsets.UTF_8);
            System.out.println(data);
        }
    }

    public void post(String url) throws Exception {
        // 创建HttpClient实例
        HttpClient client = HttpClientBuilder.create().build();
        // 根据URL创建HttpPost实例
        HttpPost post = new HttpPost(url);
        // 构造post参数
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", "11"));
        // 编码格式转换
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params);
        // 传入请求体
        post.setEntity(entity);
        // 发送请求,得到响应体
        HttpResponse response = client.execute(post);
        // 判断是否正常返回
        if (response.getStatusLine().getStatusCode() == 200) {
            // 解析数据
            HttpEntity resEntity = response.getEntity();
            String data = EntityUtils.toString(resEntity);
            System.out.println(data);
        }
    }

    public static void main(String[] args) throws Exception {
        MHttpClient cl = new MHttpClient();
        String url = "http://www.xbiquge.la/xiaoshuodaquan/";
        cl.get(url);
    }

}

六. 复杂使用方法

package com.feilong.reptile.util;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;  

import javax.net.ssl.SSLException;  

import org.apache.commons.codec.Charsets;
import org.apache.http.Header;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;  

/**
 * 使用HttpClient发送和接收Http请求
 *
 * @author manzhizhen
 *
 */
public class HttpUtils {  

    private static HttpClient httpClient;
    // 最大连接数
    private static final int MAX_CONNECTION = 100;
    // 每个route能使用的最大连接数,一般和MAX_CONNECTION取值一样
    private static final int MAX_CONCURRENT_CONNECTIONS = 100;
    // 建立连接的超时时间,单位毫秒
    private static final int CONNECTION_TIME_OUT = 1000;
    // 请求超时时间,单位毫秒
    private static final int REQUEST_TIME_OUT = 1000;
    // 最大失败重试次数
    private static final int MAX_FAIL_RETRY_COUNT = 3;
    // 请求配置,可以复用
    private static RequestConfig requestConfig;  

    static {
        SocketConfig socketConfig = SocketConfig.custom()
                .setSoTimeout(REQUEST_TIME_OUT).setSoKeepAlive(true)
                .setTcpNoDelay(true).build();  

        requestConfig = RequestConfig.custom()
                .setSocketTimeout(REQUEST_TIME_OUT)
                .setConnectTimeout(CONNECTION_TIME_OUT).build();
        /**
         * 每个默认的 ClientConnectionPoolManager 实现将给每个route创建不超过2个并发连接,最多20个连接总数。
         */
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
        connManager.setMaxTotal(MAX_CONNECTION);
        connManager.setDefaultMaxPerRoute(MAX_CONCURRENT_CONNECTIONS);
        connManager.setDefaultSocketConfig(socketConfig);  

        httpClient = HttpClients.custom().setConnectionManager(connManager)
        // 添加重试处理器
                .setRetryHandler(new MyHttpRequestRetryHandler()).build();
    }  

    public static void main(String[] args) {
        testGet();
    }  

    /**
     * 测试get方法
     */
    private static void testGet() {
        String url = "http://restapi.amap.com/v3/place/text";
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("key", "95708f902ac2428ea119ec99fb70e6a3");
        paramMap.put("keywords", "互联网金融大厦");
        paramMap.put("city", "330100");
        paramMap.put("extensions", "all");  

        try {
            System.out.println(get(url, paramMap));  

        } catch (Exception e) {
            e.printStackTrace();
        }
    }  

    /**
     * post请求
     *
     * @param url
     * @param paramMap
     * @param headers
     * @return
     * @throws Exception
     */
    public static String post(String url, Map<String, String> paramMap,
            List<Header> headers) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);
        if (paramMap != null) {
            // 添加请求参数
            for (Entry<String, String> entry : paramMap.entrySet()) {
                uriBuilder.addParameter(entry.getKey(), entry.getValue());
            }
        }  

        HttpPost httpPost = new HttpPost(uriBuilder.build());
        if (headers != null) {
            // 添加请求首部
            for (Header header : headers) {
                httpPost.addHeader(header);
            }
        }  

        httpPost.setConfig(requestConfig);  

        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);  

        return EntityUtils.toString(response.getEntity(), Charsets.UTF_8);
    }  

    /**
     * post请求,不带请求首部
     *
     * @param url
     * @param paramMap
     * @return
     * @throws Exception
     */
    public static String post(String url, Map<String, String> paramMap)
            throws Exception {  

        return post(url, paramMap, null);
    }  

    /**
     * get请求
     *
     * @param url
     * @param paramMap
     * @param headers
     * @return
     * @throws Exception
     */
    public static String get(String url, Map<String, String> paramMap,
            List<Header> headers) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);
        if (paramMap != null) {
            // 添加请求参数
            for (Entry<String, String> entry : paramMap.entrySet()) {
                uriBuilder.addParameter(entry.getKey(), entry.getValue());
            }
        }  

        HttpGet httpGet = new HttpGet(uriBuilder.build());
        if (headers != null) {
            // 添加请求首部
            for (Header header : headers) {
                httpGet.addHeader(header);
            }
        }  

        httpGet.setConfig(requestConfig);  

        // 执行请求
        HttpResponse response = httpClient.execute(httpGet);  

        return EntityUtils.toString(response.getEntity(), Charsets.UTF_8);
    }  

    /**
     * get请求,不带请求首部
     *
     * @param url
     * @param paramMap
     * @return
     * @throws Exception
     */
    public static String get(String url, Map<String, String> paramMap)
            throws Exception {  

        return get(url, paramMap, null);
    }  

    /**
     * 请求重试处理器
     * @author manzhizhen
     *
     */
    private static class MyHttpRequestRetryHandler implements
            HttpRequestRetryHandler {  

        @Override
        public boolean retryRequest(IOException exception, int executionCount,
                HttpContext context) {
            if (executionCount >= MAX_FAIL_RETRY_COUNT) {
                return false;
            }  

            if (exception instanceof InterruptedIOException) {
                // 超时
                return false;
            }
            if (exception instanceof UnknownHostException) {
                // 未知主机
                return false;
            }
            if (exception instanceof ConnectTimeoutException) {
                // 连接被拒绝
                return false;
            }
            if (exception instanceof SSLException) {
                // SSL handshake exception
                return false;
            }  

            HttpClientContext clientContext = HttpClientContext.adapt(context);
            HttpRequest request = clientContext.getRequest();
            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                // 如果请求被认为是幂等的,则重试
                return true;
            }  

            return false;
        }
    }
}  

七.运行时完整pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.feilong</groupId>
    <artifactId>reptile</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>reptile</name>
    <description>reptile for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
             <version>1.2.58</version>
        </dependency>

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.11.3</version>
</dependency>

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

原文地址:https://www.cnblogs.com/jiangfeilong/p/11109223.html

时间: 2024-10-04 12:12:45

org.apache.http.client.HttpClient使用方法的相关文章

apache.http.client.HttpClient

前言 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活.HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和

org.apache.http.client.HttpClient get/post请求

请求步骤 1.创建httpclient 对象 2.创建 请求方式,构造函数的值为请求路径 3.调用1中对象的execute() 方法,参数为 2 的对象 4.获取请求响应数据 5.释放连接资源 6.处理数据 一.使用org.apache.http.client.HttpClient 的get请求来实现 1.请求核心代码: // 创建 httpclient 对象       HttpClient httpclient = new DefaultHttpClient();    //创建请求方式,因

org.apache.http.client.HttpClient; HttpClient 4.3超时设置

可用的code public static String doPost(String url, String params, String contentType) /*throws IOException */{ CloseableHttpClient client = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(httpSoTimeout)

Android studio没有org.apache.http.client.HttpClient;等包问题 解决方案

以前用Eclipse做Android开发工具一直使用apache的http做网络请求,最近换用了Android studio发现没有办法引用apache的包,下面是我引用的步骤

解决Apache虚拟主机报错问题apache AH01630: client denied by server configuration错误解决方法

今天同事咨询通过Apache搭建创建虚拟主机,搭建好发现一直报错,提示 "apache AH01630: client denied by server configuration",在网上搜索了一下, 发现这个错误的原因是,apache2.4 与 apache2.2 的虚拟主机配置写法不同导致. apache2.2的写法: [plain] view plain copy 在CODE上查看代码片派生到我的代码片 <VirtualHost *:80> ServerName f

apache AH01630: client denied by server configuration错误解决方法

今天本来是想要在自己本地搭建一个wamp环境用来做一些代码的测试和框架的学习. 鉴于目前工作的时候用到了php5.5,所以就用了wamp-server V2.5版本,安装完成之后配置虚拟主机一直出现403,各种重启N次卸载重装,最后才好到问题的所在,故记录下来,希望其他同学有这样的问题的时候可以快速的解决~~ apache 虚拟主机配置: apache AH01630: client denied by server configuration错误解决方法 出现这个错误的原因是,apache2.

org.apache.http.client.CircularRedirectException: Circular redirect to &quot;http://xxx&quot;问题解决

org.apache.http.client.CircularRedirectException: Circular redirect to "http://xxx"问题解决 用HttpClient的时候遇到一个问题:org.apache.http.client.CircularRedirectException: Circular redirect to...解决方法如下: HttpParams params = new BasicHttpParams();  HttpGet get

jetty client 与apache http client的实现、分析

谈到httpclient的话,只要会想到apache的httpclient和jetty的httpclient,但是apache的httpclient3和4之间又有区别,通过学些,最终总结了三种方式使用HttpClient, 分别为使用httpclient3,httpclient4,jetty的httpclient,下面分别来贴代码: 第1种:使用的jar包为commons-httpclient-3.1,只需要一个jar包即可 这里使用的是GetMethod,与httpcleint4有区别 pub

HttpClient有关方法及相关优化整理

HttpClient4中采用 ThreadSafeClientConnManager来保证线程的安全,优于2.0的 MultiThreadedHttpConnectionManager类.另外Apache官方强烈推荐只使用一个HttpClient的实例,所以我 们可以将以下demo方法写成单例模式. demo将使用StringEntity来完成不指定参数名发送Post,已经采用ThreadSafeClientConnManager来保证线程的安全 ? 1 2 3 4 5 6 7 8 9 10 1