开发者

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. Ho开发者_如何学编程w 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());


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


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.


response.getEntity();

You really want to look at the Javadocs, the example for HttpClient shows you how to get at all the info in the response: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html


If you just want the content of the URL, you can use the URL API, like this:

import java.io.IOException;
import java.net.URL;
import java.util.Scanner;

public class URLTest {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://www.google.com.br");
        //here you have the input stream, so you can do whatever you want with it!
        Scanner in = new Scanner(url.openStream());
        in.nextLine();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜