在 AppEngine 中解码国际字符
我正在 Google AppEngine 中制作一个小项目,但我遇到了国际字符的问题。我的程序通过url“page.html?data1&data2...”从用户获取数据并将其存储以供稍后显示。
但是,当用户使用一些国际字符(例如 åäö)时,它会被编码为 %F4、%F5 和 %F6。我认为这是因为 http 请求中只允许使用 ASCII 表中的前 128(?) 个字符。
有没有人对此有好的解决方案?有什么简单的方法来解码文本吗?是在存储数据之前对其进行解码更好,还是在向用户显示数据时对其进行解码更好。
I'm making a small project in Google AppEngine but I'm having problems with international chars. My program takes data from the user through the url "page.html?data1&data2..." and stores it for displaying later.
But when the user are using some international characters like åäö it gets coded as %F4, %F5 and %F6. I assume it is because only the first 128(?) chars in ASCII table are allowed in http-requests.
Is there anyone who has a good solution for this? Any simple way to decode the text? And is it better to decode it before I store the data or should I decode it when displaying it to the user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
URL 可以包含任何内容,但应该编码。在Java中,您可以使用
URLEncoder
和URLDecoder
使用所需的字符编码对 url 进行编码和解码。
请记住,这些类实际上用于 HTML 表单编码,但它们可以应用于 URL 的查询字符串(参数),因此不要在整个 URL 上使用它们 - 仅在参数上使用它们。
URLs can contain anything, but it should be encoded. In Java you can use
URLEncoder
andURLDecoder
to encode and decode urls with the desired character encoding.Have in mind that these classes are actually meant for HTML form encoding, but they can be applied to the query string (the parameters) of the URLs, so do not use them on the whole URLs - only on the parameters.
URI 规范 (RFC 3986) 限制可在 URI 中使用的字符(请参阅ABNF)并定义了用于传输“不安全”的百分比编码方案“ 人物。正如 Bozho 所说,URL 的查询部分通常编码为根据 HTML 规范 (application/x-www-form -urlencoded)。
App Engine 文档指出:
因此,您或许应该让 Servlet API 为您解码参数。请参阅 HttpServletRequest 上的参数方法。这种编码通常应保留在视图层,因此数据将以未编码的方式存储。
如果您手动执行操作,请查看 这篇关于 URI 中的字符处理的博文。
The URI spec (RFC 3986) restricts the characters that can be used in URIs (see the ABNF) and defines a percent-encoding scheme for transmitting "unsafe" characters. As Bozho says, the query part of the URL is usually encoded as per the HTML spec (application/x-www-form-urlencoded).
The doc for App Engine says:
So, you should probably let the Servlet API decode the parameters for you. See the parameter methods on HttpServletRequest. This sort of encoding should generally be kept to the view layer, so data would be stored unencoded.
If you do things manually, have a look at this blog post on character handling in URIs.