URL 编码问题 - HttpWebRequest 不起作用,Firefox 起作用
我正在尝试调用 lims 系统(基本上是化学实验室数据库+接口)提供的休息网络服务。它工作得很好,直到一些>;显示了 ascii 字符(特别是带有音调、元音变音等的字符)。
当调用传递值 àèïõû 的 Web 服务时,我有以下参数:
&componentValue=àèïõû
HttpWebRequest,没有任何预转义或对值调用的 Uri.EscapeDataString() 给出:
à èïõû
Firefox ,与传递给 HttpWebRequest 的网站相同给出了正确的值:
àèïõû
现在转义本身: Uri.EscapeDataString() 似乎将“àèïõû”转义为:
%C3%A0%C3%A8%C3%AF%C3%B5%C3%BB
Firefox 将“àèõû”转义为:
%E0%E8%EF%F5%FB
由于后者有效,我当然更愿意使用它作为我的转义方法,但我真的不知道从哪里开始。我找到了大量有关处理响应数据编码的不同方法的信息,但没有找到有关请求数据的编码的信息。
I'm trying to call a rest webservice provided by a lims system (basically a chemistry lab database + interface). It was working great until some > ascii characters showed up (specifically characters with circumflexes, umlauts, etc.)
When calling the webservice passing the value àèïõû I have the following argument:
&componentValue=àèïõû
HttpWebRequest, without any pre-escaping OR with Uri.EscapeDataString() called on the value gives:
à èïõû
Firefox, with the same website as was passed to HttpWebRequest gives the correct value:
àèïõû
Now for the escaping itself:
Uri.EscapeDataString() appears to escape "àèïõû" as:
%C3%A0%C3%A8%C3%AF%C3%B5%C3%BB
Firefox escapes "àèïõû" as:
%E0%E8%EF%F5%FB
As the latter works I would of course prefer to use that as my escape method, but I really don't know where to begin. I've found plenty of information on different methods of handling encodes on the response data, but not on the request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 MSDN:
所以您看到的是
àèïõû
的 UTF-8 编码版本。与 Uri.EscapeDataString 不同,HttpUtility.UrlEncode 允许您显式指定编码:
或者,您可以编写自己的版本;例如:
两者的结果都是
“%e0%e8%ef%f5%fb”
。更好的解决方案可能是在 Web 服务中接受 UTF-8 编码的查询字符串。
From MSDN:
So what you're seeing is the UTF-8 encoded version of
àèïõû
.Unlike Uri.EscapeDataString, HttpUtility.UrlEncode allows you to specify an encoding explicitly:
Alternatively, you could write your own version; for example:
Both result in
"%e0%e8%ef%f5%fb"
.A better solution would probably be to accept UTF-8 encoded query strings in the webservice.
看来
Uri.HexEscape()
会执行您想要的操作,但一次只能执行一个字符。我会推出您自己的转义函数,并希望您的代码页始终与 Web 服务正在使用的代码页相同,因为 Web 服务似乎不支持 Unicode。It appears that
Uri.HexEscape()
will do what you want, but only one character at a time. I'd roll your own escaping function and hope that your codepage is always the same codepage that the webservice is using, since it appears that the webservice doesn't support Unicode.