HttpClient Request returns response body, warning "going to buffer response body of large or unknown size" solution

Source: Internet
Author: User

A warning is reported when a response is received by httpclient the analog POST request:

org.apache.commons.httpclient.httpmethodbase-going to buffer response body of large or unknown size. Using Getresponsebodyasstream instead is recommended.

is because the HTTP header returned does not specify ContentLength, or ContentLength is greater than the upper limit (default is 1M);

HttpClient recommends using Getresponsebodyasstream () instead of getresponsebody () to avoid the memory exhaustion problem that may be caused by getresponsebody ().

The code is as follows:

 Public Staticstring postbytextstring (string url, string text, string token) {HttpClient HttpClient=NewHttpClient (); Postmethod Postmethod=Newpostmethod (URL); Try{stringrequestentity entity=NewStringrequestentity (text, "Text/plain", Constants.charset_utf8);            Postmethod.setrequestentity (entity); Postmethod.setrequestheader ("Token", token);            Httpclient.executemethod (Postmethod); if(Postmethod.getstatuscode () = =HTTPSTATUS.SC_OK) {
          //Replace getresponsebodyasstring by reading Stream () InputStream InputStream=Postmethod.getresponsebodyasstream (); BufferedReader BR=NewBufferedReader (NewInputStreamReader (InputStream)); StringBuffer StringBuffer=NewStringBuffer (); String str; while(str = br.readline ())! =NULL) {stringbuffer.append (str); } String Response=NewString (StringBuffer);          Br.close (); //String response = new String (postmethod.getresponsebodyasstring ()); returnresponse; } Else { return NULL; } } Catch(HttpException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally{postmethod.releaseconnection (); } return NULL;

HttpClient Request returns response body, warning "going to buffer response body of large or unknown size" solution

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.