HttpGet使用错误的编码发送请求

时间:2015-11-26 07:56:57

标签: java android encoding httprequest

我正在尝试从以下网址获取文字回复:

http://translate.google.cn/translate_a/single?client=t&sl=zh-CN&tl=en&dt=t&tk=265632.142896&q=%E4%BD%A0%E5%A5%BD

回复如下:

[[["Hello there","你好",,,1]],,"zh-CN"]

(您可以通过在浏览器中输入地址来验证此响应。)

以下是我的代码的简化版本,试图下载此文本:

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class Test {
    public static String downloadString() {
        String url = "http://translate.google.cn/translate_a/single?client=t&sl=zh-CN&tl=en&dt=t&tk=265632.142896&q=%E4%BD%A0%E5%A5%BD";
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        ResponseHandler<String> handler = new BasicResponseHandler();
        try {
            return client.execute(request, handler);
        } catch (Exception e) {
            return "GET request failed.";
        }
    }
}

当我致电Test.downloadString()时,我收到以下(不正确)回复:

[[["Huan Chai Sunsolt","浣犲ソ",,,0]],,"zh-CN"]

我猜测在请求过程中的某个地方幕后存在某种编码问题(有六个字节应该被解释为两个中文字符,但是被解释为三个日文字符),但我可以似乎找不到确切的原因。我的代码中我做错了什么?

2 个答案:

答案 0 :(得分:2)

很奇怪,但添加User-Agent标头解决了问题:

request.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0");

答案 1 :(得分:0)

Android 6.0版本删除了对Apache HTTP客户端的支持。如果您的应用使用此客户端并定位到Android 2.3(API级别9)或更高版本,请改用HttpURLConnection类。

此处:http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client