httpclient4的设置方式和httpclient3有所不同,代码如下:
HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Config.20000);//连接时间20s httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);//数据传输时间60s
不设置的后果就是一旦对方服务器出现无响应的情况,如果有返回(404 50x)错误还好,没有返回了你就哭了,java线程会一直等待!!
以下是网上的:
DefaultHttpClient:
请求超时
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
读取超时
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
HttpClient
HttpClient httpClient=new HttpClient();
链接超时
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
读取超时
httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000)