I’m trying to fetch a page using Android’s DefaultHTTPClient and parse it using Jsoup. I’m getting a really weird response in which all the HTML within the <body> and </body> tags are encoded into something.
<html>
<head></head>
<body>
��������������Y�#I�&�\�+��*;����/U���53�*��U�=�D�I:I� ����X�������=H��2�`Ѓ ��o��nͽ�C瘹;�l2Y�I_l�����;f��W�k��o2.����?�r>��œ�qYξ<<<;;�g*��ѡl���9>s@I��`R��V �c�������Ɂ��e�����,> }���A�����W�?��".��ˡhޖ�Qy1�oL�_�W�h?9�E?Ofe��KO�Q��(�Av�N�h@��G�qvV�_G��W�g�'q�2�N��L�?�"鳷�x�o�����$9�}/;'#ȸ Q��&�2�\�a��aǔ�L�I�ԯ�=���TPFE� ���:�,�H�N�'QQԯ<>�i}�x��'$�'O ��qy@J�h 2��ᓃ�CH��ʤO���0�LD)��p8�챺)
</body>
</html>
Here’s my method that fetches the page.
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;
}
Why can this be happening?
If I fetch the page using Jsoup itself, the response is fine. I have to use Jsoup.connect("http://www.kat.ph/").get()
It was due to the fact that the response was GZIPped. I wired up a custom response interceptor that uncompressed the response. This is it: