疯狂编码-HTTPClient

1、导入jar包

  maven导入

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

自行下载jar包导入

 

2、编码学习

2.1 快速上手

package org.hjp;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpclientDemo {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        // 创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        // test1(httpClient, response);
        // test2(httpClient, response);
        //test3(httpClient, response);
        test4(httpClient, response);
        if (null != response) {
            response.close();
        }
        if (null != httpClient) {
            httpClient.close();
        }
    }

    /**
     * 不带参数的get请求
     * 
     * @throws Exception
     * @throws
     */
    public static void test1(CloseableHttpClient httpClient, CloseableHttpResponse response) throws Exception {
        HttpGet httpGet = new HttpGet("https://www.baidu.com");
        response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
        }
    }

    /**
     * 带参数的get请求
     * 
     * @throws Exception
     */

    public static void test2(CloseableHttpClient httpClient, CloseableHttpResponse response) throws Exception {

        URI url = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "httpclient").build();
        HttpGet httpGet = new HttpGet(url);
        response = httpClient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
        }
    }

    public static void test3(CloseableHttpClient httpClient, CloseableHttpResponse response) throws Exception {

        HttpPost httpPost = new HttpPost("https://www.baidu.com");
        response = httpClient.execute(httpPost);
        // 伪装成浏览器
        httpPost.setHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");

        System.out.println(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
        }
    }

    public static void test4(CloseableHttpClient httpClient, CloseableHttpResponse response) throws Exception {

        HttpPost httpPost = new HttpPost("https://www.baidu.com/s");
        response = httpClient.execute(httpPost);
        // 伪装成浏览器
        httpPost.setHeader(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");
        List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
        parameters.add(new BasicNameValuePair("scope", "project"));
        parameters.add(new BasicNameValuePair("wd", "java"));
        parameters.add(new BasicNameValuePair("fromerr", "7nXH76r7"));
        // 构造一个form表单式的实体
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
        // 将请求实体设置到httpPost对象中
        httpPost.setEntity(formEntity);

        System.out.println(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
        }
    }
}

2.3连接管理器

package org.hjp;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class HttpConnectManager {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PoolingHttpClientConnectionManager cm =new PoolingHttpClientConnectionManager();
        //设置最大连接数
        cm.setMaxTotal(20);
        //设置每个主机地址的并发访问数
        cm.setDefaultMaxPerRoute(20);
        //获取HTTPClient
        CloseableHttpClient build = HttpClients.custom().setConnectionManager(cm).build();
    }

}

2.4 定时关闭无效连接

package org.hjp;

import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class ClientEvictExpiredConnections {

    public static void main(String[] args) throws Exception {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        // 设置最大连接数
        cm.setMaxTotal(200);
        // 设置每个主机地址的并发数
        cm.setDefaultMaxPerRoute(20);

        new IdleConnectionEvictor(cm).start();
    }

    //定时关闭无效的链接
    public static class IdleConnectionEvictor extends Thread {

        private final HttpClientConnectionManager connMgr;

        private volatile boolean shutdown;

        public IdleConnectionEvictor(HttpClientConnectionManager connMgr) {
            this.connMgr = connMgr;
        }

        @Override
        public void run() {
            try {
                while (!shutdown) {
                    synchronized (this) {
                        wait(5000);
                        // 关闭失效的连接
                        connMgr.closeExpiredConnections();
                    }
                }
            } catch (InterruptedException ex) {
                // 结束
            }
        }

        public void shutdown() {
            shutdown = true;
            synchronized (this) {
                notifyAll();
            }
        }
    }
}

3、HTTPClient和spring整合

3.1 配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 定义连接管理器 -->
	<bean id="httpClientConnectionManager"
		class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
		<property name="maxTotal" value="${http.maxTotal}" />
		<property name="defaultMaxPerRoute" value="${http.defaultMaxPerRoute}" />
	</bean>

	<!-- httpclient的构建器 -->
	<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder">
		<property name="connectionManager" ref="httpClientConnectionManager" />
	</bean>

	<!-- 定义Httpclient对象 -->
	<!-- 该对象是多例的 -->
	<bean class="org.apache.http.impl.client.CloseableHttpClient"
		factory-bean="httpClientBuilder" factory-method="build" scope="prototype">
	</bean>

	<!-- 请求参数的构建器 -->
	<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig.Builder">
		<!-- 创建连接的最长时间 -->
		<property name="connectTimeout" value="${http.connectTimeout}" />
		<!-- 从连接池中获取到连接的最长时间 -->
		<property name="connectionRequestTimeout" value="${http.connectionRequestTimeout}" />
		<!-- 数据传输的最长时间 -->
		<property name="socketTimeout" value="${http.socketTimeout}" />
		<!-- 提交请求前测试连接是否可用 -->
		<property name="staleConnectionCheckEnabled" value="${http.staleConnectionCheckEnabled}" />
	</bean>

	<!-- 定义请求参数对象 -->
	<bean class="org.apache.http.client.config.RequestConfig"
		factory-bean="requestConfigBuilder" factory-method="build" />

	<!-- 定期关闭无效连接 -->
	<bean class="com.taotao.web.httpclient.IdleConnectionEvictor">
		<constructor-arg index="0" ref="httpClientConnectionManager" />
	</bean>

</beans>

4、工具类

package org.hjp;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class ApiService implements BeanFactoryAware{

    @Autowired
    private RequestConfig requestConfig;
    

    /**
     * 指定GET请求,返回:null,请求失败,String数据,请求成功
     * 
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String doGet(String url) throws ClientProtocolException, IOException {
        // 创建http GET请求
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);
        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = getHttpClient().execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return null;
    }

    /**
     * 带有参数的GET请求,返回:null,请求失败,String数据,请求成功
     * 
     * @param url
     * @param params
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     * @throws URISyntaxException
     */
    public String doGet(String url, Map<String, String> params) throws ClientProtocolException, IOException,
            URISyntaxException {
        URIBuilder builder = new URIBuilder(url);
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.setParameter(entry.getKey(), entry.getValue());
        }
        return this.doGet(builder.build().toString());
    }

    /**
     * 带有参数的POST请求
     * 
     * @param url
     * @param params
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public HttpResult doPost(String url, Map<String, String> params) throws ParseException, IOException {
        // 创建http POST请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        if (null != params) {
            // 设置post参
            List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
            for (Map.Entry<String, String> entry : params.entrySet()) {
                parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            // 构造一个form表单式的实体
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
            // 将请求实体设置到httpPost对象中
            httpPost.setEntity(formEntity);
        }

        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = getHttpClient().execute(httpPost);
            return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(
                    response.getEntity(), "UTF-8"));
        } finally {
            if (response != null) {
                response.close();
            }
        }
    }

    /**
     * 没有参数的POST请求
     * 
     * @param url
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public HttpResult doPost(String url) throws ParseException, IOException {
        return this.doPost(url, null);
    }
    
    private CloseableHttpClient getHttpClient(){
        //通过Bean工厂获取bean,保证HttpClient对象是多例
        return this.beanFactory.getBean(CloseableHttpClient.class);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`jbundle-util-osgi-wrapped-httpclient` 是一个用于在 OSGi(开放服务网关倡议)环境中使用 Apache HttpClient 的工具库。它提供了将 Apache HttpClient 封装为 OSGi 插件的功能,使得在 OSGi 容器中可以方便地使用 HttpClient 进行 HTTP 请求和处理。 Apache HttpClient 是一个功能强大的 HTTP 客户端库,用于在 Java 应用程序中进行 HTTP 通信。它提供了丰富的功能和灵活的配置选项,包括支持各种 HTTP 方法、处理认证和代理、处理 Cookie、支持连接池等。 使用 `jbundle-util-osgi-wrapped-httpclient` 可以进行以下操作: 1. 封装 Apache HttpClient:`jbundle-util-osgi-wrapped-httpclient` 将 Apache HttpClient 封装为 OSGi 插件,使得它可以在 OSGi 容器中进行动态加载和管理。这样你就可以通过 OSGi 的方式来使用 HttpClient,而无需手动处理其依赖和生命周期管理。 2. OSGi 配置和服务:工具库提供了一些实用方法和类,用于在 OSGi 环境中配置和使用 HttpClient。它提供了 OSGi 配置管理器和服务注册的支持,使得你可以方便地配置和获取 HttpClient 实例。 3. HTTP 请求和响应处理:`jbundle-util-osgi-wrapped-httpclient` 提供了一些工具类和方法,用于发送 HTTP 请求和处理响应。你可以使用这些工具来构建和发送 HTTP 请求,并处理服务器返回的响应数据。 需要注意的是,`jbundle-util-osgi-wrapped-httpclient` 是一个针对 OSGi 环境的工具库,主要用于在 OSGi 容器中使用 Apache HttpClient。如果你不使用 OSGi,可以直接使用 Apache HttpClient 的原生库。 希望这个解释对你有帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值