大家好,又见面了,我是你们的朋友全栈君。
httpclient4的设置方式和httpclient3有所不同,代码如下:
HttpClient httpclient = new DefaultHttpClient();httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Config.20000);//连接时间20shttpclient.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)
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/163268.html原文链接:https://javaforall.cn