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