So I'm using Apache Commons HTTP to make a request to a webpage. I cannot for the life of me figure out how to get the actual content from the page, I can just get its header information. How can I get the actual content from it?
Here is my example code:
HttpGet request = new HttpGet("http://URL_HERE/");
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
System.out.println("Response: " + response.toString());
createDefault() Creates CloseableHttpClient instance with default configuration. static CloseableHttpClient. createMinimal() Creates CloseableHttpClient instance that implements the most basic HTTP protocol support.
CloseableHttpClient is the base class of the httpclient library, the one all implementations use. Other subclasses are for the most part deprecated. The HttpClient is an interface for this class and other classes. You should then use the CloseableHttpClient in your code, and create it using the HttpClientBuilder .
Description copied from interface: HttpAsyncClientInitiates asynchronous HTTP request execution using the given context. The request producer passed to this method will be used to generate a request message and stream out its content without buffering it in memory.
Use HttpResponse#getEntity()
and then HttpEntity#getContent()
to obtain it as an InputStream
.
InputStream input = response.getEntity().getContent();
// Read it the usual way.
Note that HttpClient isn't part of Apache Commons. It's part of Apache HttpComponents.
BalusC's comment will work just fine.
If you're using version 4 or newer of Apache HttpComponents, there is a convenience method you can use as well:
EntityUtils.toString(HttpEntity);
Here's what it'll look like in your code:
HttpGet request = new HttpGet("http://URL_HERE/");
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String entityContents = EntityUtils.toString(entity);
Not sure if this is due to different versions, but I had to rewrite it like this:
HttpGet request = new HttpGet("http://URL_HERE/");
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String entityContents = EntityUtils.toString(entity);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With