I am retrieving a webpage using Apache httpcomponents Java library . After connecting the result, I get HttpEntityone that has a method getContent()that returns InputStream, and also has a method writeTo()that writes to OutputStream.
HttpEntity
getContent()
InputStream
writeTo()
I want to turn the result into a String to extract the information. What is the most elegant (and safest) way to do this?
Some possible solutions:
ByteArrayOutputStream
Both of them feel a little ugly. Would you recommend choosing one of them or something else?
How about (pseudo):
BasicResponseHandler handler = new org.apache.http.impl.client.BasicResponseHandler (); String str = httpClient.execute(request, handler);
In this case, you have to handle the exceptions yourself.
System.out.println (EntityUtils.toString (httpResponse.getEntity ()));
, , . IOUtils.toString() Commons-IO, .