Skip to content
Advertisement

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());

Advertisement

Answer

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.

Advertisement