DefaultHttpClient在多个请求上保持活动连接 [英] DefaultHttpClient keep alive connection on multiple requests

查看:99
本文介绍了DefaultHttpClient在多个请求上保持活动连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DefaultHttpClient通过基本身份验证向同一个URL发出大量请求。

I'm using the DefaultHttpClient to make numerous requests to the same URL with basic authentication.

这样的事情:

for (String json: listOfItems)
{
    DefaultHttpClient client = new DefaultHttpClient();

    try
    {       
       client.getCredentialsProvider().setCredentials(
                    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "basic"),
                    new UsernamePasswordCredentials(user, pass));

       HttpPost request = new HttpPost(path);
       setHeaders(request);

       StringEntity se = new StringEntity(json, HTTP.UTF_8);
       se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
       request.setEntity(se);

       client.execute(request);    
    }       
    finally
    {
       // close/release connection
       client.getConnectionManager().shutdown();            
    }
}

我的问题是保持活力的最好方法是什么这样做时的连接。
所以我不需要关闭每个帖子请求的连接。

My question is what is the best way to keep alive the connection while doing this. So I don't need to close the connection on each post request.

推荐答案

您可能需要添加Socket_Timeout参数并实现KeepAlive策略,这里详细说明了这一点 -

You might need to add the Socket_Timeout parameter and implement a KeepAlive strategy, which are detailed over here -

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e652

这篇关于DefaultHttpClient在多个请求上保持活动连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆