Java-commons-httpclient timeout settings setconnectiontimeout and setsotimeout

Source: Internet
Author: User
Tags stringbuffer

Problem

Request data before using HttpClient

Source Method:

     Public StaticString dohttp (httpmethod result,intTimeout, String charset) {HttpClient Client=NewHttpClient (); Try{httpconnectionmanagerparams Managerparams=Client.gethttpconnectionmanager (). Getparams ();            Managerparams.setconnectiontimeout (timeout);            Client.executemethod (result); InputStream Resstream=Result.getresponsebodyasstream (); BufferedReader BR=NewBufferedReader (NewInputStreamReader (Resstream, CharSet)); StringBuffer Resbuffer=NewStringBuffer (); String restemp= "";  while((Restemp = Br.readline ())! =NULL) {resbuffer.append (restemp); }            returnresbuffer.tostring (); } Catch(HttpException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } finally{result.releaseconnection (); }        return NULL; }

Found one in

Client.executemethod (result);

A card will not come out here, the reason lies in the

Managerparams.setconnectiontimeout (timeout); // Timeout for connection

The connection timeout is used here, and I am missing a data timeout here, otherwise it will wait until the data is returned so that the request data timeout code is added below.

The following is the complete code: (where the red part is the protagonist)

     Public StaticString dohttp (httpmethod result,intTimeout, String charset) {HttpClient Client=NewHttpClient (); Try{httpconnectionmanagerparams Managerparams=Client.gethttpconnectionmanager (). Getparams ();            Managerparams.setconnectiontimeout (timeout); managerparams.setsotimeout (timeout); //wait for result timeoutClient.executemethod (Result); InputStream Resstream=Result.getresponsebodyasstream (); BufferedReader BR=NewBufferedReader (NewInputStreamReader (Resstream, CharSet)); StringBuffer Resbuffer=NewStringBuffer (); String restemp= "";  while((Restemp = Br.readline ())! =NULL) {resbuffer.append (restemp); }            returnresbuffer.tostring (); } Catch(HttpException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } finally{result.releaseconnection (); }        return NULL; }

See Introduction

This article describes the http://jinnianshilongnian.iteye.com/blog/2089792 from

Parameter settings 1, httpclient 4.2.3
Httpparams params =Newbasichttpparams ();//Setting the connection time-out periodInteger connection_timeout = 2 * 1000;//Set Request Timeout 2 seconds based on business tuningInteger so_timeout = 2 * 1000;//set the wait data time-out time to 2 seconds based on business tuning//defines the millisecond time-out period used when retrieving managedclientconnection instances from Clientconnectionmanager//This parameter expects to get a value of type Java.lang.Long. If this parameter is not set, the default equals Connection_timeout, so be sure to setLong conn_manager_timeout = 500L;//the value is to wait for timeout when the connection is not enough, be sure to set it, and not too big ()Params.setintparameter (coreconnectionpnames.connection_timeout, Connection_timeout);p Arams.setintparameter ( Coreconnectionpnames.so_timeout, So_timeout);p arams.setlongparameter (Clientpnames.conn_manager_timeout, CONN_ Manager_timeout);//test whether a connection is available before submitting a requestParams.setbooleanparameter (Coreconnectionpnames.stale_connection_check,true); Poolingclientconnectionmanager Conmgr=NewPoolingclientconnectionmanager (); Conmgr.setmaxtotal (200);//set the total connection pool maximum number of connections depending on your scenario//is the default maximum connection for a route (the value defaults to 2), and the limit quantity actually used Defaultmaxperroute is not maxtotal. //set too small to support large concurrency (connectionpooltimeoutexception:timeout waiting for connection from pool), routing is a subdivision of Maxtotal. Conmgr.setdefaultmaxperroute (Conmgr.getmaxtotal ());//(there is currently only one route, so let him be equal to the maximum value)//In addition, set the number of retries of the HTTP client, which is 3 by default, and is currently disabled (this is the default if the project volume is not available)Httpclient.sethttprequestretryhandler (NewDefaulthttprequestretryhandler (0,false));

the differences between Maxttotal and Defaultmaxperroute are explained here:

1, Maxttotal is the size of the whole pond; 2, Defaultmaxperroute is a subdivision of Maxtotal according to the host connected to; For example: maxttotal=400 defaultmaxperroute= 200 while I am only connected to http://xx.com, the concurrency to this host is only 200, not 400; and I connect to http://xx.com and http://  xxx.com, the maximum concurrency for each host is only 200, which adds up to 400 (but not more than 400); So the setting that works is Defaultmaxperroute. 2, HttpClient 3.1
Httpconnectionmanagerparams params =Newhttpconnectionmanagerparams ();p arams.setconnectiontimeout (2000);p Arams.setsotimeout (2000);//Maximum number of connectionsParams.setmaxtotalconnections (500);p Arams.setdefaultmaxconnectionsperhost (500);p arams.setstalecheckingenabled (true); Connectionmanager.setparams (params); Httpclientparams Httpclientparams=Newhttpclientparams ();//Setting the connection timeout for httpclient, connection timeout set for Connection Manager is uselessHttpclientparams.setconnectionmanagertimeout (5000);//equivalent to the conn_manager_timeout in 4.2.3HttpClient =NewHttpClient (ConnectionManager); Httpclient.setparams (httpclientparams);//In addition, set the number of retries of the HTTP client, which is 3 by default, and is currently disabled (this is the default if the project volume is not available)Httpclientparams.setparameter (Httpmethodparams.retry_handler,NewDefaulthttpmethodretryhandler (0,false)); 

the parameters are similar and are not explained much;

Another look at the content of the reprint

HttpClient 4 and HttpClient 3 set timeout


HttpClient 4:
Connection timeout:

Httpclient.getparams (). Setparameter (coreconnectionpnames.connection_timeout,60000);

Or

Httpconnectionparams.setconnectiontimeout (params, 6000);


Read timeout:

Httpclient.getparams (). Setparameter (coreconnectionpnames.so_timeout,60000);

Or

Httpconnectionparams.setsotimeout (params, 60000);


HttpClient 3:
Connection timeout:

Httpclient.gethttpconnectionmanager (). Getparams (). Setconnectiontimeout (60000);

Read timeout:

Httpclient.gethttpconnectionmanager (). Getparams (). Setsotimeout (60000);

Java-commons-httpclient timeout settings setconnectiontimeout and setsotimeout

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.