一般在使用HttpClient时,我们提前设置好参数,比如超时时间(一般socket超时和连接超时)
private DefaultHttpClient createHttpClient() { //代码1
ThreadSafeClientConnManager connectMag = new ThreadSafeClientConnManager();
...
client = new DefaultHttpClient(connectMag);
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
"...");
client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
2000);
client.getParams().setIntParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);
return client;
}
httpGet.getParams().setIntParameter(
CoreConnectionPNames.SO_TIMEOUT, 5000);
httpGet.getParams().setIntParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.execute(httpGet, new BasicResponseHandler());