HttpClients4.* version timeout settings

http://www.open-open.com/lib/view/open1383751765321.html

HttpClient 4.3. HttpClient is the same as Lucene, and the API changes a lot with each version, which is a bit of a headache. It's like creating an HttpClient object, each version is different,

3.X is like this

HttpClient httpClient=new DefaultHttpClient();
4.3 is like this
CloseableHttpClient httpClient = HttpClients.createDefault();
Of course, the above changes are just There are some small changes, and everyone will know it if you look at the API.
What I want to talk about is the timeout setting. HttpClient has three kinds of timeout settings. I have been busy recently, and I have no time to summarize it in detail. I will add it later. Here I will talk about some of the simplest and most easy-to-use timeout setting methods.

This is a 3.X timeout setting method

HttpClient client = new HttpClient();
client.setConnectionTimeout(30000); 
client.setTimeout(30000);
HttpClient httpClient= new HttpClient(); 
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout (5000);
4.X version timeout setting (obsolete after 4.3)
HttpClient httpClient=new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,2000);//Connection time
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,2000);//Data transfer time
4.3 version timeout Set
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet=new HttpGet("http://www.baidu.com");//HTTP Get request (same as POST)
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000 ).setConnectTimeout(2000).build();//Set the request and transmission timeout
httpGet.setConfig(requestConfig);
httpClient.execute(httpGet);//Execute the request
BTW, if the 4.3 version does not set the timeout, once the server does not respond , the waiting time is N long (>24 hours).


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326691549&siteId=291194637
Recommended