有没有办法使用UTF-8与应用程序引擎? [英] Is there a way to use UTF-8 with app engine?

查看:92
本文介绍了有没有办法使用UTF-8与应用程序引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找关于应用引擎如何处理字符编码的一些解释。我正在开发一个客户端 - 服务器应用程序,其中服务器在应用程序引擎上。

I'm looking for some explanation on how the app engine deals with character encodings. I'm working on a client-server application where the server is on app engine.

这是一个从头开始构建的新应用程序,因此我们使用UTF-8到处。客户端通过POST,x-www-form-urlencoded向服务器发送一些字符串。我收到他们并回应他们。当客户端回来,它的ISO-8859-1!我也看到这个行为当POSTing到Blobstore,参数发送为UTF-8,多部分/表单数据编码。

This is a new application built from scratch, so we're using UTF-8 everywhere. The client sends some strings to the server through POST, x-www-form-urlencoded. I receive them and echo them back. When the client gets it back, it's ISO-8859-1! I also see this behavior when POSTing to the blobstore, with the parameters sent as UTF-8, multipart/form-data encoded.

为了记录,我看到这在Wireshark。所以我100%确定我发送UTF-8并接收ISO-8859-1。另外,我没有看到mojibake:ISO-8859-1编码的字符串是完全精细。这也不是错误解释Content-Type的问题。这不是客户端。在某种程度上,正确地识别我发送UTF-8参数,但是由于某种原因转换为ISO-8859-1。

For the record, I'm seeing this in Wireshark. So I'm 100% sure I send UTF-8 and receive ISO-8859-1. Also, I'm not seeing mojibake: the ISO-8859-1 encoded strings are perfectly fine. This is also not an issue of misinterpreting the Content-Type. It's not the client. Something along the way is correctly recognizing I'm sending UTF-8 parameters, but is converting them to ISO-8859-1 for some reason.

认为ISO-8859-1是GAE servlet的默认字符编码。我的问题是,有没有办法告诉GAE不转换为ISO-8859-1,而是使用UTF-8无处不在?

I'm led to believe ISO-8859-1 is the default character encoding for the GAE servlets. My question is, is there a way to tell GAE not to convert to ISO-8859-1 and instead use UTF-8 everywhere?

让我们说servlet做某事这个:

Let's say the servlet does something like this:

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("application/json");
    String name = req.getParameter("name");
    String json = "{\"name\":\"" + name + "\"}";
    resp.getOutputStream().print(json);
}



我尝试将响应的字符编码设置为UTF-8

I tried setting the character encoding of the response and request to "UTF-8", but that didn't change anything.

提前感谢,

推荐答案

找到了解决方法。这是我怎么做的:

Found a way to work around it. This is how I did it:


  • 使用application / json; charset = UTF-8作为内容类型。或者,将响应字符集设置为UTF-8(无论如何都可以正常工作,无需两者)。

  • Used "application/json; charset=UTF-8" as the content-type. Alternatively, set the response charset to "UTF-8" (either will work fine, no need to do both).

Base64编码输入字符串不是ASCII安全的,并且作为UTF-8。

Base64-encoded the input strings that aren't ASCII-safe and come as UTF-8. Otherwise they get converted to ISO-8859-1 when they get to the servlet, apparently.

使用resp.getWriter()而不是resp.getOutputStream()方法来转换为ISO-8859-1。打印JSON响应。

Used resp.getWriter() instead of resp.getOutputStream() to print the JSON response.

全部之后,能够将UTF-8输出回客户端。

After all those conditions were met, I was finally able to output UTF-8 back to the client.

这篇关于有没有办法使用UTF-8与应用程序引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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