httpPost设置连接超时
这个要怎么设置连接超时,不是响应超时,HttpMethodParams.SO_TIMEOUT我看网上写的这个代表请求超时,求指教
引用文字
回答:
你是用的HttpClient版本是多少?不通版本下设置超时有些差异,已为HttpClient 4.5
例:
CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://stackoverflow.com/");
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000).setConnectionRequestTimeout(1000)
.setSocketTimeout(5000).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = httpclient.execute(httpGet);
System.out.println("得到的结果:" + response.getStatusLine());//得到请求结果
HttpEntity entity = response.getEntity();//得到请求回来的数据
推荐你使用HttpClient配合使用Fluent-API:
https://hc.apache.org/httpcom...
设置超时:
// Execute a GET/POST with timeout settings and return response content as String.Request.Get("http://somehost/")
.connectTimeout(1000)
.socketTimeout(1000)
.execute().returnContent().asString();
以上是 httpPost设置连接超时 的全部内容, 来源链接: utcz.com/p/174485.html