DefaultHttpClient 在多个请求上保持活动连接 [英] DefaultHttpClient keep alive connection on multiple requests
本文介绍了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();
}
}
我的问题是在执行此操作时保持连接活动的最佳方法是什么.所以我不需要在每个 post 请求上关闭连接.
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屋!
查看全文