HttpClient 4 设置超时 httpclient 超时有时httpclient的时候,需要等待N长时间,可能此时你决定放弃或者重试。实现上非常简单 添加一个参数即可 httpClient.getParams().setIntParameter("http.socket.timeout",3000); 这里的超时单位是毫秒。
这里的http.socket.timeout相当于SO_TIMEOUT
HttpConnectionManagerParams managerParams = httpClient
.getHttpConnectionManager().getParams();
// 设置连接超时时间(单位毫秒)
managerParams.setConnectionTimeout(30000);
// 设置读数据超时时间(单位毫秒)
managerParams.setSoTimeout(120000);
1,设置get方法请求超时为 5 秒
1.GetMethod getMethod= new GetMethod(url);
2.getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,5000 );
2,设置 Http 连接超时为5秒
1.HttpClient httpClient= new HttpClient();
2. httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000 );
设置连接超时和请求超时,这两个超时的意义不同,需要分别设置。