如何在 Java 的 HTTP 响应中更改字符集编码 [英] How can i change charset encoding in HTTP response in Java

查看:52
本文介绍了如何在 Java 的 HTTP 响应中更改字符集编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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;


    }

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

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.

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

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

我使用 eclipse 进行调试,并注意到wrappedEntity 下的字符集显示为US-ASCII".我附上一个 jpg 以供参考

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

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

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 .

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

Please zoom in to see the image more clearly

提前致谢

推荐答案

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

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"));

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

这篇关于如何在 Java 的 HTTP 响应中更改字符集编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆