HttpComponents组件特性探究
作者:网络转载 发布时间:[ 2012/11/26 10:37:29 ] 推荐标签:
return result;
}
/**
* @param uri
* @param nameValuePairs
* @return
*/
public static PostMethod createPostMethod(String uri, NameValuePair[] nameValuePairs) {
PostMethod method = new PostMethod(uri);
method.addParameters(nameValuePairs);
method.getParams().setContentCharset(charsetName);
return method;
}
/**
* @param uri
* @param nameValuePairs
* @return
*/
public static GetMethod createGetMethod(String uri, NameValuePair[] nameValuePairs) {
GetMethod method = new GetMethod(uri);
List list = Lists.newArrayList();
if (nameValuePairs != null) {
Collections.addAll(list, nameValuePairs);
method.setQueryString(list.toArray(new NameValuePair[nameValuePairs.length]));
}
method.getParams().setContentCharset(charsetName);
return method;
}
public static HttpClient createHttpClient() {
//1.
HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
//2.
HttpConnectionManagerParams httpConnectionManagerParams = httpClient
.getHttpConnectionManager().getParams();
httpConnectionManagerParams.setConnectionTimeout(connectionTimeout);
httpConnectionManagerParams.setTcpNoDelay(true);//Nagle's algorithm
httpConnectionManagerParams.setSoTimeout(timeOut);
httpConnectionManagerParams.setDefaultMaxConnectionsPerHost(maxHostConnections);
httpConnectionManagerParams.setMaxTotalConnections(maxTotalConnections);
//3.
HttpClientParams httpClientParam = httpClient.getParams();
//httpClientParam.setConnectionManagerTimeout(connectionTimeout);//暂且不关注这个超时设置,后面根据性能酌情考虑
httpClientParam.setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(retryCount, false));
httpClientParam.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
return httpClient;
}
public static JSONObject doGet(String url, NameValuePair[] params) {
return executeMethod(createHttpClient(), createGetMethod(url, params));
}
public static JSONObject doPost(String url, NameValuePair[] params) {
return executeMethod(createHttpClient(), createPostMethod(url, params));
}
protected HttpClientUtils() {
}
public void setTimeOut(int timeOut) {
HttpClientUtils.timeOut = timeOut;
}
public static int getTimeOut() {
return timeOut;
}
public static int getRetryCount() {
return retryCount;
}
public void setRetryCount(int retryCount) {
HttpClientUtils.retryCount = retryCount;
}
public static int getConnectionTimeout() {
return connectionTimeout;
}
public void setConnectionTimeout(int connectionTimeout) {
HttpClientUtils.connectionTimeout = connectionTimeout;
}
public static int getMaxHostConnections() {
return maxHostConnections;
}
public void setMaxHostConnections(int maxHostConnections) {
HttpClientUtils.maxHostConnections = maxHostConnections;
}
public static int getMaxTotalConnections() {
return maxTotalConnections;
}
public void setMaxTotalConnections(int maxTotalConnections) {
HttpClientUtils.maxTotalConnections = maxTotalConnections;
}
public static String getCharsetName() {
return charsetName;
}
public void setCharsetName(String charsetName) {
HttpClientUtils.charsetName = charsetName;
}
}
好了,有了活生生的代码,我们来总结一下httpClient封装过程中需要注意的一些事项吧。恩,其实更多的是体现在安全,性能上面:
(1)多线程模型,尤其注意finally中collection的释放问题。除此之外,需要考虑池化连接的异常处理,这是我文中提到特别注意的三大异常之一;
(2)Retry机制中对幂等性的处理。尤其是在httpClient4中,put和post操作,未按照http规范行事,需要我们额外注意;
(3)SSL、TLS的定制化处理;
(4)并发标记的处理,这里使用了Concurrency in practice中的并发annotation,有什么用?感兴趣的朋友可以了解下SureLogic,别问我要license,因为俺也不是apache开源社区的developer呀;
(5)拦截器对header的处理;
(6)collection stale check机制;
(7)Cookie specification choose或者是自定义实现;
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11