如何使用 HTTP 标头发送非英文 unicode 字符串? [英] How to send non-English unicode string using HTTP header?

查看:29
本文介绍了如何使用 HTTP 标头发送非英文 unicode 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 HTTP 相关问题的新手.我的问题是在 iOS 开发中,我想使用 HTTP Header 发送一个字符串,所以我使用的是:

I am novice to HTTP related matters. My question is in iOS development, I would like to send a string using HTTP Header, so I'm using:

[httpRequest setValue:@"nonEnglishString" forHTTPHeaderField:@"customHeader"];

接收服务器是 Python(Google App Engine),将 db 模型中的字符串值保存为 StringProperty 使用:

The receiving server is Python(Google App Engine), saving the string value in the db model as StringProperty using:

dataEntityInstance.nonEnglishString = unicode(self.request.headers.get('customHeader')

但是,问题是当我尝试发送像韩语这样的非英语字符串时,它会像这样保存在 HTTP 标头中:

However, the problem is when I try to send non-English string like Korean, it's saved in HTTP header like this:

Customheader = "Uc8fcUba39Uc774 Uc6b4Ub2e4";

当它被 Google App Engine 接收并保存在 DataStore 中时,它变为:

and when it's received by Google App Engine and saved in DataStore, it's changed to be like:

??? ??

好像找不到 unicode 值的正确字符.

as if it can't find the proper characters for the unicode value.

是否不能或不允许使用 HTTP Header 发送非英文字符串?

如果我的 iOS 只使用 setHTTPBody,它可以传输非英文字符串并正确保存到 App Engine 的 DataStore.

If my iOS uses just setHTTPBody, it can transfer non-English strings and save to App Engine's DataStore properly.

[httpRequest setHTTPBody:[httpBody dataUsingEncoding:NSUTF8StringEncoding]];

但我只是找不到使用 HTTP 标头实现相同目标的正确方法,就像 Foursquare 等许多 API 所做的那样,并将字符串以适当的形式保存在基于 Python 的 Google App Engine 的 DataStore 中

But I just can't find the right way to achieve same goal using HTTP Headers, like what many APIs like Foursquare's do and saving the strings in the proper forms in Python based Google App Engine's DataStore

推荐答案

使用 HTTP Header 发送非英文字符串是不可能或不允许的吗?

Is it not POSSIBLE or ALLOWED to send non-English string using HTTP Header?

根据 HTTP 标准,将非 ISO-8859-1 字符直接放入 HTTP 标头是不可能的.这为您提供了 ASCII(英语"?)字符以及常见的西欧变音符号.

It's not possible as per HTTP standards to put non-ISO-8859-1 characters directly in an HTTP header. That gives you ASCII ("English"?) characters plus common Western European diacriticals.

但是在实践中,您甚至不能使用扩展的 ISO-8859-1 字符,因为服务器和浏览器在如何处理标头中的非 ASCII 字符方面并不一致.Safari 采用 RFC2616 字样并将高字节视为 ISO-8859-1 字符;Mozilla 采用 UTF-16 代码单元低字节,类似但更奇怪;Opera 和 Chrome 从 UTF-8 解码;IE 使用本地系统代码页.

However in practice you can't even use the extended ISO-8859-1 characters, because servers and browsers don't agree on what to do with non-ASCII characters in headers. Safari takes RFC2616 at its word and treats high bytes as ISO-8859-1 characters; Mozilla takes UTF-16 code unit low bytes, which is similar but weirder; Opera and Chrome decode from UTF-8; IE uses the local system code page.

所以实际上,您可以在 HTTP 标头中输入的只是没有控制代码的简单 ASCII.如果你想要更多,你必须想出一个编码方案(例如 UTF-8+base64).RFC2616 标准建议将 RFC2047 编码词作为一种标准的编码形式,但考虑到 RFC2047 本身何时允许使用它们的定义,这是没有意义的,并且没有任何支持.

So in reality all you can put in an HTTP header is simple ASCII with no control codes. If you want anything more, you'll have to come up with an encoding scheme (eg UTF-8+base64). The RFC2616 standard suggests RFC2047 encoded-words as a standard form of encoding, but this makes no sense given the definitions of when they are allowable in RFC2047 itself, and nothing supports it.

这篇关于如何使用 HTTP 标头发送非英文 unicode 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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