1. Differences between httpclient4.3 and set timeout in earlier versions:
Reference: http://my.oschina.net/u/577453/blog/173724
Resolution: If no timeout is set, it may take a long time to get, causing the program to be suspended (hanging and not ending)
I recently used httpclient to write Weibo crawlers. I found that creating an httpclient object is different in every version,
3. X is like this.
| 1 |
Httpclient =New Defaulthttpclient (); |
4.3.
| 1 |
Closeablehttpclient httpclient = httpclients. createdefault (); |
These changes are not very large, but the timeout settings. There are three timeout settings for httpclient, which will change a lot. Here are some of the simplest and most easy-to-use timeout settings.
This is a 3. x timeout setting method.
| 123 |
Httpclient client =New Httpclient ();Client. setconnectiontimeout (30000);Client. setTimeout (30000); |
| 12 |
Httpclient =New Httpclient ();Httpclient. gethttpconnectionmanager (). getparams (). setconnectiontimeout (5000); |
4. x version timeout settings (expired after 4.3)
| 123 |
Httpclient =New Defaulthttpclient ();Httpclient. getparams (). setparameter (coreconnectionpnames. connection_timeout,2000);// Connection timeHttpclient. getparams (). setparameter (coreconnectionpnames. so_timeout,2000);// Data transmission time |
4.3 timeout settings
| 12345 |
closeablehttpclient httpclient = httpclients. createdefault (); httpget = New httpget ( " http://www.baidu.com " ); // http get request (similar to post) requestconfig = requestconfig. custom (). setsockettimeout ( 2000 ). setconnecttimeout ( 2000 ). build (); // set the request and transmission timeout httpget. setconfig (requestconfig); httpclient.exe cute (httpget ); // execute the request |
More importantly, if the timeout value is not set for version 4.3, once the server does not respond, it will take a long time (> 24 hours) to wait !.
Httpclient Operation Summary