如何在 Java 的 HTTP 响应中更改字符集编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18046505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:02:57  来源:igfitidea点击:

How can i change charset encoding in HTTP response in Java

javahttpcharacter-encodinghttpresponseapache-httpclient-4.x

提问by bourne

I have to fetch some JSON object from a remote server and for that i am using this function which is working great except that for sometime some weird data is getting fetched which i believe is because it is using ASCII charset to decode.

我必须从远程服务器获取一些 JSON 对象,为此我正在使用这个功能很好的函数,但有时会获取一些奇怪的数据,我相信这是因为它使用 ASCII 字符集进行解码。

Please find below thw method that i am using

请在下面找到我正在使用的方法

public HttpResponse call(String serviceURL,String serviceHost,String namespace,String methodName,String payloadKey, String payloadValue) throws ClientProtocolException,IOException,JSONException
    {
            HttpResponse response = null;
            HttpContext HTTP_CONTEXT = new BasicHttpContext();
            HTTP_CONTEXT.setAttribute(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0");
            HttpPost httppost = new HttpPost(serviceURL);
            httppost.setHeader("User-Agent",Constants.USER_AGENT_BROWSER_FIREFOX);
            httppost.setHeader("Accept", "application/json, text/javascript, */*");
            httppost.setHeader("Accept-Language","en-US,en;q=0.8");
            httppost.setHeader("Content-Encoding", "foo-1.0");
            httppost.setHeader("Content-Type", "application/json; charset=UTF-8");
            httppost.setHeader("X-Requested-With","XMLHttpRequest");
            httppost.setHeader("Host",serviceHost);
            httppost.setHeader("X-Foo-Target", String.format("%s.%s", namespace,methodName));
            /*Making Payload*/
            JSONObject objectForPayload = new JSONObject();
            objectForPayload.put(payloadKey, payloadValue);
            StringEntity stringentity = new StringEntity(objectForPayload.toString());
            httppost.setEntity(stringentity);
            response = client.execute(httppost);
            return response;


    }

All these headers that i am passing are correct and i have verified the same via inspect element in Google chrome or Firebug plugin if you are familiar with Mozilla.

我传递的所有这些标头都是正确的,如果您熟悉 Mozilla,我已经通过 Google chrome 或 Firebug 插件中的检查元素验证了相同的信息。

Now the problem is that most of the time i am getting the readable data but sometimes i do get unreadable data.

现在的问题是,大部分时间我都得到了可读的数据,但有时我确实得到了不可读的数据。

I debugged using eclipse and noticed that the charset under wrappedEntity is showing as "US-ASCII". I am attaching a jpg for referenceenter image description here

我使用 eclipse 进行调试,并注意到wrappedEntity 下的字符集显示为“US-ASCII”。我附上一张 jpg 以供参考在此处输入图片说明

Can someone please tell me how can i change the charset from ASCII to UTF-8 of the response before i do response = client.execute(httppost);. PS:As you have noticed that i am passing charset=utf-8 in the header and that i have already verified using firebug and google chrome that i am passing the exact headers .

有人可以告诉我如何在我做之前将响应的字符集从 ASCII 更改为 UTF-8 response = client.execute(httppost);。PS:正如您所注意到的,我在标头中传递了 charset=utf-8 并且我已经使用 firebug 和 google chrome 验证了我正在传递确切的标头。

Please zoom in to see the image more clearly

请放大以更清楚地查看图像

Thanks in advance

提前致谢

回答by geby

You may need to add an "Accept-Encoding"-header and set this to "UTF-8"

您可能需要添加“接受编码”标头并将其设置为“UTF-8”

回答by lscoughlin

I don't think it's a problem with your headers, I think it's a problem with your string. Just having the header say it's utf-8 doesn't mean the string you write is utf-8, and that depends a lot on how the string was encoded and what's in the "payloadValue"

我不认为这是你的标题的问题,我认为这是你的字符串的问题。只是让标题说它是 utf-8 并不意味着你写的字符串是 utf-8,这在很大程度上取决于字符串的编码方式以及“payloadValue”中的内容

That said, you can always re-encode the thing correctly before sending it across the wire, for example:

也就是说,您始终可以在通过网络发送之前正确地重新编码事物,例如:

objectForPayload.put(payloadKey, payloadValue);
StringEntity stringentity = new StringEntity(
   new String(
      objectForPayload.toString().getBytes(),
      "UTF8"));

See if that works for you.

看看这是否适合你。

回答by bourne

i was able to resolve the issue just mentioning it for people that may face similar issue. after getting the response first get the entity by using HttpEntity entity = response.getEntity();and since my response was a json object convert entity to string but using "UTF-8" something like this responseJsonObject = new JSONObject(EntityUtils.toString(entity,"UTF-8"));

我能够解决这个问题,只是为可能面临类似问题的人提到它。获得响应后首先通过使用获取实体 HttpEntity entity = response.getEntity();,因为我的响应是一个 json 对象,将实体转换为字符串,但使用“UTF-8”这样的东西 responseJsonObject = new JSONObject(EntityUtils.toString(entity,"UTF-8"));

previously i was just doing responseJsonObject = new JSONObject(EntityUtils.toString(entity));

以前我只是在做 responseJsonObject = new JSONObject(EntityUtils.toString(entity));

回答by Julian Reschke

Just for the record: the "Content-Encoding" header field is incorrect - a correct server would reject the request as it contains an undefined content coding format.

只是为了记录:“内容编码”标头字段不正确 - 正确的服务器会拒绝请求,因为它包含未定义的内容编码格式。

Furthermore, attaching a charset parameter to application/json is meaningless.

此外,将字符集参数附加到 application/json 是没有意义的。