网上搜索httpclient超时设置:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet=new HttpGet("http://www.baidu.com");//HTTP Get请求(POST雷同)
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();//设置请求和传输超时时间
httpGet.setConfig(requestConfig);
httpClient.execute(httpGet);//执行请求
但这么设置实际执行之后并未生效,后改为如下设置,超时设置生效:
HttpGet httpGet=new HttpGet("http://www.baidu.com");//HTTP Get请求(POST雷同)
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();//设置请求和传输超时时间
httpGet.setConfig(requestConfig);
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
httpClient.execute(httpGet);//执行请求
HTTPClient超时设置技巧

本文探讨了在使用Apache HttpClient时如何正确设置超时参数。通过对比两种不同的配置方法,指出仅在请求中设置超时可能不会生效,并分享了一种确保超时设置生效的实践方案。
862

被折叠的 条评论
为什么被折叠?



