Httpclient Operation Summary

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.