HttpClient use today encountered a giant pit --HttpEntity content not get out

In use HttpPost httpPost = new HttpPost (postUrl); after post request, get the response returns, returns 200 OK response.

 

This without any problems.

HttpEntity response returned content objects using received:

// get back the response data object from 
the HttpEntity Entity response.getEntity = (); 

// print the response content
ResultStr = EntityUtils.toString (response.getEntity (), "UTF-. 8");
System.out.println (ResultStr) ;

 

 

So the question is, fuck, entity objects in the content can be printed out, there is something. But I want to get him the data stream: InputStream is = entity.getContent (); // get the return of the pdf file stream

which is the input stream is empty. . . . empty. . . . empty. . . . .

 

I do not believe is empty, print entity object Content-Length:

. System OUT .println ( " file about: " + (entity.getContentLength () / 1024 ) + " K " ); // print out is 0k, I speak, unreasonable ah 

System. OUT .println ( " of the type: " + entity.getContentType () + " , length: " + entity.getContentLength ()); // object type is application / pdf, the length is -1, that is empty. . . . . 


Width can not, obviously hell to print the contents of the above, the length is empty, the input stream IS read () -1 read data is empty, Baidu reasons have not found for a long time


 

 

Solution is found, but the contents are read HttpEntity empty a little confused why. It seems to be using

EntityUtils.toString (response.getEntity (), "UTF-8"); this method, this method of removing the same ah, anyway confuse

 

Solution, I want to get response.getEntity (); in the data, can only use the following method:

byte [] = byteData EntityUtils.toByteArray (response.getEntity ()); // get the return result byte stream

 

Byte stream is able to get, it seems can only handle a byte stream

 

Guess you like

Origin www.cnblogs.com/spll/p/11857835.html