我正在尝试使用HTTPClient获取页面,但是它似乎返回乱码(我认为是二进制)。
这是我的代码:
public Boolean getPage(String url, String referer) {
httpClient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
httpClient.getParams().setParameter("http.socket.timeout", new Integer(1000));
httpClient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
HttpGet httpGet = new HttpGet(url);
response = null;
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3");
httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpGet.setHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
httpGet.setHeader("Accept-Encoding", "gzip,deflate");
httpGet.setHeader("Referer", referer);
int tryNumber = 0;
while(tryNumber<5){
tryNumber++;
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
ret = httpClient.execute(httpGet,responseHandler).toString();
Log.v("Info:", ret);
}
catch(Exception e) {
error = e;
break;
}
}
}
谁能指出我要去哪里了?
我正在尝试获取此页面的内容:http://hosh.me.uk/test.php
查看屏幕截图:http://img.ctrlv.in/4ecd69c40a590.jpg
霍什
最佳答案
只是一个猜测,但这可能是响应被压缩了吗?也许您应该删除以下行,以便您的回复为纯文本。
httpGet.setHeader("Accept-Encoding", "gzip,deflate");