`
85977328
  • 浏览: 1871000 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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<String> query = new ArrayList<String>();
        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);
2
5
分享到:
评论
2 楼 85977328 2013-07-30  
clxy 写道
这段代码需要改进下。

try {  
    HttpGet get = new HttpGet("http://search.panguso.com/searchword.htm?q=" + item);  
    HttpResponse response = client.execute(get);  
    print(response);  
    get.releaseConnection();  
} catch (Exception e) {  
    e.printStackTrace();  
}  


改进版
HttpGet get = new HttpGet("http://search.panguso.com/searchword.htm?q=" + item);  
try {  
    HttpResponse response = client.execute(get);  
    print(response);  
} catch (Exception e) {
    get.abort();
    e.printStackTrace();  
} finally{
    get.releaseConnection();
}


那个print方法也需要处理资源的close,不如用HC自带的方法好。
System.out.println(EntityUtils.toString(response.getEntity()));  



非常感谢,学习了!!!! 有机会多多交流
1 楼 clxy 2013-07-30  
这段代码需要改进下。

try {  
    HttpGet get = new HttpGet("http://search.panguso.com/searchword.htm?q=" + item);  
    HttpResponse response = client.execute(get);  
    print(response);  
    get.releaseConnection();  
} catch (Exception e) {  
    e.printStackTrace();  
}  


改进版
HttpGet get = new HttpGet("http://search.panguso.com/searchword.htm?q=" + item);  
try {  
    HttpResponse response = client.execute(get);  
    print(response);  
} catch (Exception e) {
    get.abort();
    e.printStackTrace();  
} finally{
    get.releaseConnection();
}


那个print方法也需要处理资源的close,不如用HC自带的方法好。
System.out.println(EntityUtils.toString(response.getEntity()));  

相关推荐

Global site tag (gtag.js) - Google Analytics