文档章节

HttpClient的使用

A__17
 A__17
发布于 2017/08/21 23:10
字数 1109
阅读 26
收藏 0

HttpClient的使用:

1)httpClient的简单使用:

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

import org.apache.http.NameValuePair;
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.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 {

		// 1.创建Httpclient对象
		CloseableHttpClient httpclient = HttpClients.createDefault();

		// 2.1创建http GET请求,并设置参数
		String url = "http://www.jxn.com/";
		URI uri = new URIBuilder(url).setParameter("name", "jack").setParameter("age", "1").build();
		HttpGet httpGet = new HttpGet(uri);

		// 2.2创建http POST请求,并设置参数
		HttpPost httpPost = new HttpPost(url);
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("name", "jack"));
		parameters.add(new BasicNameValuePair("age", "1"));
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
		httpPost.setEntity(formEntity);
				
		// 3.构建请求配置信息
		RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) // 创建连接的最长时间
				.setConnectionRequestTimeout(500) // 从连接池中获取到连接的最长时间
				.setSocketTimeout(10 * 1000) // 数据传输的最长时间
				.setStaleConnectionCheckEnabled(true) // 提交请求前测试连接是否可用
				.build();
		httpGet.setConfig(config);
		httpPost.setConfig(config);

		CloseableHttpResponse response = null;
		try {
			// 4.执行请求
			response = httpclient.execute(httpGet);
//            response = httpclient.execute(httpPost);
						
			if (response.getStatusLine().getStatusCode() == 200) { // 判断返回状态是否为200
				String content = EntityUtils.toString(response.getEntity(), "UTF-8");
				System.out.println(content);
			}	
		} finally {
			if (response != null) {
				response.close();
			}
			httpclient.close();
		}
	}
}

2)使用httpClient连接池:

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

public class HttpClientConnectionManagerDemo {

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

		// 执行一次get请求
		doGet(connectionManager);
	}

	public static void doGet(HttpClientConnectionManager connectionManager) throws Exception {
		CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();

		String url = "http://www.jxn.com/";
		HttpGet httpGet = new HttpGet(url);

		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);
			}
		} finally {
			if (response != null) {
				response.close();
			}
			// 此处不能关闭httpClient!如果把httpClient关闭掉,则连接池也会被销毁
			// httpClient.close();
		}
	}
}

3)HttpClient与Spring整合:

【1】pom.xml文件:

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

【2】spirng配置文件applicationContext-httpclient.xml:

<!-- 定义httpClient连接管理器 -->
<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.jxn.common.httpclient.IdleConnectionEvictor">
	<constructor-arg index="0" ref="httpClientConnectionManager" />
</bean>

属性配置文件httpclient.properties:
	http.maxTotal=200
	http.defaultMaxPerRoute=50
	http.connectTimeout=1000
	http.connectionRequestTimeout=500
	http.socketTimeout=10000
	http.staleConnectionCheckEnabled=true

【3】封装成Service:

import java.io.IOException;
import java.net.URISyntaxException;
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.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jxn.common.httpclient.HttpResult;

[@Service](https://my.oschina.net/service)
public class ApiService implements BeanFactoryAware {

	private BeanFactory beanFactory;

	@Autowired(required = false)
	private RequestConfig requestConfig;
	
	
	[@Override](https://my.oschina.net/u/1162528)
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}

	private CloseableHttpClient getHttpClient() {
		return this.beanFactory.getBean(CloseableHttpClient.class);
	}
	
	// 执行GET请求
	public String doGet(String url) throws ClientProtocolException, IOException {

		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请求
	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 doGet(builder.build().toString());
	}

	// 执行post请求
	public HttpResult doPost(String url, Map<String, String> params) throws IOException {
		// 创建http POST请求
		HttpPost httpPost = new HttpPost(url);
		httpPost.setConfig(requestConfig);
		if (null != params) {
			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, "UTF-8");
			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请求,发送json数据
	public HttpResult doPostJson(String url, String json) throws IOException {
		// 创建http POST请求
		HttpPost httpPost = new HttpPost(url);
		httpPost.setConfig(requestConfig);
		if (null != json) {
			// 构造一个字符串的实体
			StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
			httpPost.setEntity(stringEntity);
		}

		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();
			}
		}
	}
}

// 返回的结果
public class HttpResult {

	private Integer code;
	private String data;

	public HttpResult() {
	}
	public HttpResult(Integer code, String data) {
		this.code = code;
		this.data = data;
	}
	public Integer getCode() {
		return code;
	}
	public void setCode(Integer code) {
		this.code = code;
	}
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}
}

【4】实现定期关闭无效的连接:

import org.apache.http.conn.HttpClientConnectionManager;

public class IdleConnectionEvictor extends Thread {

	private final HttpClientConnectionManager connectionManager;

	private volatile boolean shutdown;

	// 创建IdleConnectionEvictor对象的时候,启动线程
	public IdleConnectionEvictor(HttpClientConnectionManager connectionManager) {
		this.connectionManager = connectionManager;
		this.start();
	}

	[@Override](https://my.oschina.net/u/1162528)
	public void run() {
		try {
			while (!shutdown) {
				synchronized (this) {
					wait(5000);
					// 关闭失效的连接(每隔5秒检查一次)
					connectionManager.closeExpiredConnections();
				}
			}
		} catch (InterruptedException ex) {
			// 结束
		}
	}

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

© 著作权归作者所有

共有 人打赏支持
A__17
粉丝 2
博文 100
码字总数 117532
作品 0
朝阳
私信 提问
Android 浅谈HttpClient工具类

在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务。你可以把HttpClient想象成一个浏览器,通过它的...

Jonson
2013/07/25
0
3
HttpComponents Client 4.5.4,Java 的 HTTP 协议库

HttpComponents Client 4.5.4 已发布,修复了一些发现自 4.5.3 的问题。要注意的是需要 Java 1.6 及以上版本。更新内容如下: [HTTPCLIENT-1883] SystemDefaultCredentialsProvider to use ...

王练
2017/12/05
925
3
Apache HttpClient 资源释放、请求超时,导致线程池用光、内存不足

Apache HttpClient,据说很强大,可以支持峰值一秒7万以上的请求。不过需要注意一些资源释放和超时处理的问题。 问题1:线程资源无法释放,最终导致内存不足、或线程池被用光。 问题代码: ...

onedotdot
10/17
0
0
.NET Core 2.1中的HttpClientFactory最佳实践

ASP.NET Core 2.1中出现一个新的HttpClientFactory功能, 它有助于解决开发人员在使用HttpClient实例从其应用程序发出外部Web请求时可能遇到的一些常见问题。 介绍 在.NETCore平台的2.1新增了...

dotNET跨平台
08/16
0
0
.NetCore 2.1中的HttpClientFactory最佳实践

.NET Core 2.1中的HttpClientFactory最佳实践 ASP.NET Core 2.1中出现一个新的HttpClientFactory功能, 它有助于解决开发人员在使用HttpClient实例从其应用程序发出外部Web请求时可能遇到的一...

Chaunce
08/14
0
0

没有更多内容

加载失败,请刷新页面

加载更多

jquery通过id显示隐藏

var $div3 = $('#div3'); 显示 $div3.show(); 隐藏 $div3.hide();

yan_liu
今天
3
0
《乱世佳人》读书笔记及相关感悟3900字

《乱世佳人》读书笔记及相关感悟3900字: 之前一直听「荔枝」,后来不知怎的转向了「喜马拉雅」,一听就是三年。上班的时候听房产,买房了以后听装修,兴之所至时听旅行,分手后听亲密关系,...

原创小博客
今天
3
0
大数据教程(9.6)map端join实现

上一篇文章讲了mapreduce配合实现join,本节博主将讲述在map端的join实现; 一、需求 实现两个“表”的join操作,其中一个表数据量小,一个表很大,这种场景在实际中非常常见,比如“订单日志...

em_aaron
今天
3
0
cookie与session详解

session与cookie是什么? session与cookie属于一种会话控制技术.常用在身份识别,登录验证,数据传输等.举个例子,就像我们去超市买东西结账的时候,我们要拿出我们的会员卡才会获取优惠.这时...

士兵7
今天
3
0
十万个为什么之为什么大家都说dubbo

Dubbo是什么? 使用背景 dubbo为什么这么流行, 为什么大家都这么喜欢用dubbo; 通过了解分布式开发了解到, 为适应访问量暴增,业务拆分后, 子应用部署在多台服务器上,而多台服务器通过可以通过d...

尾生
今天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部