java 中网络发送的请求超时如何设置?
ConnManagerParams.setTimeout(params, 1000);
我查到的这个方法已经被废弃了,我的代码
headers.add(new BasicHeader("User-Agent", "Mozilla/5.0(Windows NT 6.1;Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"));
headers.add(new BasicHeader("Referer", "https://gongshang.mingluji.com/beijing/"));
HttpClientBuilder httpClient = HttpClientBuilder.create().setDefaultHeaders(headers);
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.build().execute(httpGet);
TimeUnit.SECONDS.sleep(SleepTime);
HttpEntity httpEntity = response.getEntity();
ResponseBody = EntityUtils.toString(httpEntity, "utf-8");
###如果是使用apache的httpclient发送http请求,请求的超时时间是通过socket设置的,可参考如下代码:
CloseableHttpClient client = HttpClients.custom()
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(500) //连接超时时间,单位毫秒,按实际情况调整
.build())
.setDefaultSocketConfig(SocketConfig.custom()
.setSoTimeout(2 * 1000) //请求响应超时时间,单位毫秒,这里设为2秒,按实际情况调整
.build())
.build();
###RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(500000).setConnectionRequestTimeout(100000).setSocketTimeout(500000).build();
httpGet.setConfig(requestConfig);