httpcomponents-client-4.2.4专业的HTTP客户端封装

在Java领域,谈到网络编程,可能大家脑海里第一反应就是MINA,NETTY,GRIZZLY等优秀的开源框架。没错,不过在深入探究这些框架之前,我们需要先从最original的技术探究开始(当然,需要大家先熟悉java.net.*类库)。这里,我要和大家分享一下HttpComponents项目的部分组件特性。HttpClient,想必大家早都接触过了吧。HttpComponents和HttpClient的”血缘“有点像guava和google-collection的关系。目前,HttpComponents已经是Apache的顶级项目了,它旨在为我们提供一个Http协议相关的Java平台工具集。

package com.panguso.phl.release;

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

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class TestPerformance {
private static HttpClient client = new DefaultHttpClient();

public static void main(String[] args) {
List query = new ArrayList();
query.add("z");
query.add("zh");
query.add("zho");
query.add("zhon");
query.add("zhong");
for (String item : query) {
try {
HttpGet get = new HttpGet("http://search.panguso.com/searchword.htm?q=" + item);
HttpResponse response = client.execute(get);
System.out.println(EntityUtils.toString(response.getEntity()));
get.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}



        HttpClient httpClient = new SystemDefaultHttpClient();
HttpClientUtil.setMaxConnections(httpClient, 10000);
HttpClientUtil.setMaxConnectionsPerHost(httpClient, 1000);
HttpClientUtil.setConnectionTimeout(httpClient, 3000);

你可能感兴趣的:(Apache-Commons)