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

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

问题描述

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

  [httpRequest setValue:@nonEnglishStringforHTTPHeaderField:@customHeader]; 

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

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

然而,问题是当我尝试发送非英文字符串像韩文时,它被保存在HTTP标头中,如下所示:

  Customheader =\Uc8fc\Uba39\Uc774 \Uc6b4\Ub2e4; 

,当它被Google App Engine收到并保存在DataStore中时,它会更改为:

  ??? 

犹如它不能找到unicode值的正确字符。



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



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

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

但是我无法找到使用HTTP Headers实现相同目标的正确方法, APIs像Foursquare的do和在基于Python的Google App Engine的DataStore中以正确的格式保存字符串

解决方案


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


根据HTTP标准, -ISO-8859-1字符直接放在HTTP头中。这给你ASCII(英语?)字符加上西欧常用的变音符号。然而,在实践中,你甚至不能使用扩展的ISO-8859-1字符,因为服务器和浏览器不同意如何处理标题中的非ASCII字符。 Safari采用RFC2616,并将高字节视为ISO-8859-1字符; Mozilla采用UTF-16编码单元的低字节,这是相似的,但更加奇怪;来自UTF-8的Opera和Chrome解码; IE使用本地系统代码页。



因此,实际上,所有可以放入HTTP标头的都是简单的ASCII码,不带控制码。如果你想要更多的东西,你将不得不提出一种编码方案(例如UTF-8 + base64)。 RFC2616标准建议RFC2047编码词作为标准的编码形式,但是这对于RFC2047本身允许的定义是没有意义的,并且没有任何支持。


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

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')

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

Customheader = "\Uc8fc\Uba39\Uc774 \Uc6b4\Ub2e4";

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

??? ??

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

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

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]];

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

解决方案

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

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.

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.

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