DefaultHttpClient 使用GZIPInputStream解压缩
当浏览器访问网站时,有可能浏览器返回的消息头中带有 Content-Encoding:gzip,表明服务器返回的消息经过gzip压缩,这么做是为了节省流量,浏览器拿到gzip压缩后的http包,对其进行解压缩,再渲染出来。在使用apache提供的DefaultHttpClient操作http请求时,可以使用GZIPInputStream对gzip压缩过的数据包进行解压缩。android sdk进行网络编程时,也可以使用这种方法。简单代码如下:
DefaultHttpClient httpClient = new DefaultHttpClient();
//dns探测源ip
String[] dnsIps = {"jsdx", "yndx", "bjdx", "bjlt", "sclt", "shlt", "gg"};
for(String ip : dnsIps)
{
HttpPost post = new HttpPost("http://webscan.360.cn/tools/dnsInfo.php");
post.addHeader("Referer", "http://webscan.360.cn/tools/dnslookup");
post.addHeader("X-Requested-With", "XMLHttpRequest");
post.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
post.addHeader("Accept", "*/*");
post.addHeader("Accept-Encoding", "gzip, deflate");
post.addHeader("Accept-Language", "zh-cn,en-us;q=0.7,en;q=0.3");
post.addHeader("Pragma", "no-cache");
post.addHeader("Cache-Control", "no-cache");
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("dns_ip", ip));
params.add(new BasicNameValuePair("domain_name", domain));
params.add(new BasicNameValuePair("dns_type", "A"));
UrlEncodedFormEntity formEntity = null;
HttpResponse response = null;
String responseHtml = null;
try
{
formEntity = new UrlEncodedFormEntity(params, "utf-8");
post.setEntity(formEntity);
response = httpClient.execute(post);
InputStream is= response.getEntity().getContent();
is= new GZIPInputStream(is);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer sb = new StringBuffer();
while((line = br.readLine())!=null) {
sb.append(line);
}
responseHtml = sb.toString();
} catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
版权声明
本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。
© 空空博客,本文链接:https://www.yeetrack.com/?p=692
近期评论