0

我正在尝试使用 Android 获取页面DefaultHTTPClient并使用 Jsoup 解析它。我得到了一个非常奇怪的响应,其中<body>and</body>标记中的所有 HTML 都被编码成某种东西。

<html>
  <head></head>
  <body>
       ��������������Y�#I�&amp;amp;�\�+��*;����/U���53�*��U�=�D�I:I� ����X�����΃��=H��2�`Ѓ  ��o��nͽ�C瘹;�l2Y�I_l�����;f��W�k��o2.����?�r&gt;��œ�qYξ&lt;&lt;&lt;;;�g*��ѡl���9&gt;s@I��`R��V �c�������Ɂ��e�����,&gt; }���A�����W�?��&quot;.��ˡhޖ�Qy1�oL�_�W�h?9�E?Ofe��KO�Q��(�Av�N�h@��G�qvV�_G��W�g�'q�2�N��L�?�&amp;quot;鳷�x�o�����$9�}/;'#ȸ Q��&amp;�2�\�a��aǔ�L�I�ԯ�=���TPFE� ���:�,�H�N�'QQԯ&lt;&gt;�i}�x��'$�'O ��qy@J�h 2��ᓃ�CH��ʤO���0�LD)��p8�챺)
  </body>
</html>

这是我获取页面的方法。

  public String doGet(String strUrl, List<NameValuePair> lstParams) throws Exception {

          String strResponse = null;
          HttpGet htpGet = new HttpGet(strUrl);
          //htpGet.addHeader("Accept-Encoding", "gzip, deflate");
          htpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1");
          DefaultHttpClient dhcClient = new DefaultHttpClient();
          PersistentCookieStore pscStore = new PersistentCookieStore(this.objContext);
          dhcClient.setCookieStore(pscStore);
          HttpResponse resResponse = dhcClient.execute(htpGet);
          strResponse = EntityUtils.toString(resResponse.getEntity());
          return strResponse;

  }

为什么会发生这种情况?

如果我使用 Jsoup 本身获取页面,则响应很好。我必须使用Jsoup.connect("http://www.kat.ph/").get()

4

2 回答 2

1

试试这个方法……结果一样吗……

URL url = new URL("Your_URL");

InputStream is = url.openStream();   // or url.openConnection();

Scanner scan = new Scanner(is);

while(scan.hasNextLine()){

 System.out.println(scan.nextLine());


 }

}
于 2012-09-09T15:11:12.150 回答
1

这是因为响应是 GZIPped。我连接了一个自定义响应拦截器来解压缩响应。就是这个:

class Decompressor implements HttpResponseInterceptor {

    /*
     * @see org.apache.http.HttpResponseInterceptor#process(org.apache.http.
     * HttpResponse, org.apache.http.protocol.HttpContext)
     */
    public void process(HttpResponse hreResponse, HttpContext hctContext)   throws HttpException, IOException {

        HttpEntity entity = hreResponse.getEntity();

        if (entity != null) {

            Header ceheader = entity.getContentEncoding();

            if (ceheader != null) {

                HeaderElement[] codecs = ceheader.getElements();

                for (int i = 0; i < codecs.length; i++) {

                    if (codecs[i].getName().equalsIgnoreCase("gzip")) {

                        hreResponse.setEntity(new HttpEntityWrapper(entity) {

                            @Override
                            public InputStream getContent() throws IOException, IllegalStateException {

                                return new GZIPInputStream(wrappedEntity.getContent());

                            }

                            @Override
                            public long getContentLength() {

                                return -1;

                            }

                        });

                        return;

                    }

                }

            }

        }

    }

}
于 2012-09-11T09:35:19.177 回答