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

查看:24
本文介绍了如何使用 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 发送非英文字符串是不是 POSSIBLE 或 ALLOWED?

如果我的 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 发送非英文字符串是不是 POSSIBLE 或 ALLOWED?

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天全站免登陆