HttpClient的3种超时
Posted siwnchh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpClient的3种超时相关的知识,希望对你有一定的参考价值。
原文:http://www.cnblogs.com/codingmyworld/archive/2011/08/17/2141706.html
HttpClient的3种超时说明
/* 从连接池中取连接的超时时间 */
ConnManagerParams.setTimeout(params, 1000);
/* 连接超时 */
HttpConnectionParams.setConnectionTimeout(params, 2000);
/* 请求超时 */
HttpConnectionParams.setSoTimeout(params, 4000);
第一行设置ConnectionPoolTimeout:这定义了从ConnectionManager管理的连接池中取出连接的超时时间,此处设置为1秒。
第二行设置ConnectionTimeout: 这定义了通过网络与服务器建立连接的超时时间。Httpclient包中通过一个异步线程去创建与服务器的socket连接,这就是该socket连接的超时时间,此处设置为2秒。
第三行设置SocketTimeout: 这定义了Socket读数据的超时时间,即从服务器获取响应数据需要等待的时间,此处设置为4秒。
示例1
package edu.cdut.robin;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
publicclassCustomerHttpClient
{
privatestaticfinalString CHARSET = HTTP.UTF_8;
privatestaticHttpClient customerHttpClient;
privateCustomerHttpClient()
{
}
publicstaticsynchronizedHttpClient getHttpClient()
{
if(null== customerHttpClient)
{
HttpParamsparams=newBasicHttpParams();
/* 设置一些基本参数 */
HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, CHARSET);
HttpProtocolParams.setUseExpectContinue(params,true);
HttpProtocolParams
.setUserAgent(
params,
"Mozilla/5.0(Linux;U;android 2.2.1;en-us;Nexus One Build.FRG83) "
+"AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1");
/* 超时设置 */
/* 从连接池中取连接的超时时间 */
ConnManagerParams.setTimeout(params,1000);
/* 连接超时 */
HttpConnectionParams.setConnectionTimeout(params,2000);
/* 请求超时 */
HttpConnectionParams.setSoTimeout(params,4000);
/* 设置我们的HttpClient支持HTTP和HTTPS两种模式 */
SchemeRegistry schReg =newSchemeRegistry();
schReg.register(newScheme("http",PlainSocketFactory
.getSocketFactory(),80));
schReg.register(newScheme("https",SSLSocketFactory
.getSocketFactory(),443));
/* 使用线程安全的连接管理来创建HttpClient */
ClientConnectionManager conMgr =newThreadSafeClientConnManager(
params, schReg);
customerHttpClient =newDefaultHttpClient(conMgr,params);
}
return customerHttpClient;
}
}
示例2
void doHttpGet(String userAgent,Context context,String uri)
{
AndroidHttpClient client =null;
client =AndroidHttpClient.newInstance(userAgent, context);
HttpParamsparams= client.getParams();
HttpConnectionParams.setConnectionTimeout(params,30000);
HttpConnectionParams.setSoTimeout(params,60000);
HttpClientParams.setRedirecting(params,true);
HttpClientParams.setAuthenticating(params,false);
HttpResponse response;
HttpGet request =newHttpGet(uri);
try
{
response = client.execute(request);
}catch(IllegalArgumentException ex)
{
}catch(IOException ex)
{
}
}
结束!
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://www.cnblogs.com/captainbed
以上是关于HttpClient的3种超时的主要内容,如果未能解决你的问题,请参考以下文章