Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get page content from Apache Commons HTTP Request

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());
like image 393
Chiggins Avatar asked Mar 09 '11 01:03

Chiggins


People also ask

What is HttpClients createDefault ()?

createDefault() Creates CloseableHttpClient instance with default configuration. static CloseableHttpClient. createMinimal() Creates CloseableHttpClient instance that implements the most basic HTTP protocol support.

What is CloseableHttpClient?

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 .

What is Closeablehttpasyncclient?

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.


2 Answers

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.

like image 108
BalusC Avatar answered Nov 09 '22 20:11

BalusC


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);
like image 45
SecondSun24 Avatar answered Nov 09 '22 19:11

SecondSun24