0

我正在尝试使用 Apache HttpClient 检索此页面:http: //quick-dish.tablespoon.com/

不幸的是,当我尝试这样做时,它只返回以下内容(由 JSoup 返回,因此可能它实际上只是返回 HTTP... 字符串本身):

<html>
 <head></head>
 <body>
  HTTP/1.1 200 OK [Server: nginx/1.0.11, Content-Type: text/html;charset=UTF-8, Last-Modified: Mon, 02 Jul 2012 15:30:40 GMT, Vary: Accept-Encoding, Cookie,Accept-Encoding, X-Powered-By: PHP/5.3.6, X-Pingback: http://quick-dish.tablespoon.com/xmlrpc.php, X-Powered-By: ASP.NET, Content-Encoding: gzip, X-Blz: lb1.blaze.io, Date: Mon, 02 Jul 2012 16:06:21 GMT, Content-Length: 11723, Connection: keep-alive]
 </body>
</html>

这是我的代码(请注意,我正在模拟 Google Bot,因为我发现 Web 服务器的行为往往更好):

URL sourceURL = new URL("http://quick-dish.tablespoon.com/");
HttpClient httpClient =  new ContentEncodingHttpClient();
httpClient.getParams().setBooleanParameter("http.protocol.handle-redirects", true);

final HttpGet httpget = new HttpGet(sourceURL.toURI());
httpget.setHeader("User-Agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
httpget.setHeader("Accept", "text/html");
httpget.setHeader("Accept-Charset", "utf-8");

final HttpResponse response = httpClient.execute(httpget);
return Jsoup.parse(response.toString());

不用说,该页面在我的网络浏览器中返回正常。有任何想法吗?

4

2 回答 2

2

您需要获取响应实体而不是 toString

// Get hold of the response entity
 HttpEntity entity = response.getEntity();

然后你可以得到它的内容

于 2012-07-02T16:21:25.857 回答
0
HttpEntity entity = response.getEntity();
String pageHTML = EntityUtils.toString(entity);
Jsoup.parse(response.toString());
于 2012-09-24T09:29:43.970 回答