当查询字符串是 URLEncoded 或在 ASP.NET 中具有百分比字符时如何获取查询字符串 [英] How to get a querystring when it is URLEncoded or has percent characters in ASP.NET

查看:26
本文介绍了当查询字符串是 URLEncoded 或在 ASP.NET 中具有百分比字符时如何获取查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当查询字符串是 UrlEncoded 或在 ASP.NET 中有百分比字符时,如何从 Request 对象中获取实际的查询字符串?

How to get the actual querystring from the Request object when the querystring is UrlEncoded or has percent characters in ASP.NET?

基本上,如果我有一个像这样的 URL:Default.aspx?p=%b4,我如何得到一个用%b4"填充的字符串?

Basicly, if I have a Url like this: Default.aspx?p=%b4, how do I get a string populated with "%b4"?

Request.QueryString["p"] 返回一个不可打印的字符.

Request.QueryString["p"] returns a non printable character.

Request.RawUrl 返回 Default.aspx?p=%ufffd"

Request.RawUrl returns Default.aspx?p=%ufffd"

Request.Url.AbsoluteUri 返回 Default.aspx?p=%EF%BF%BD

Request.Url.AbsoluteUri returns Default.aspx?p=%EF%BF%BD

我怎样才能取回%b4"?

How can I get "%b4" back?

推荐答案

我进一步研究了这个问题,相信我知道是什么导致了这个问题:一个 HTTP 客户端正在向服务器提交一个 URL,它没有正确地进行 URL 编码.具体来说,URL 中存在无效字符.

I dug into this further, and believe I know what's causing this: an HTTP client is submitting a URL to the server which is not properly URL-encoded. Specifically, there is an invalid character in the URL.

要重现,请将以下内容粘贴到 IE8 的 URL 末尾:default.aspx?p=´

To repro, paste the following at the end of your URL into IE8: default.aspx?p=´

如果您检查通过网络传输的字节(例如使用 Fiddler),您会看到实际的十六进制 B4 字符正在 URL 中从客户端发送到服务器.这是 URL 中的非法字符,因为 URL 仅限于 0x80 以下的字符代码(任何大于 0x80 的字符代码都必须进行百分比转义).

If you examine the bytes going over the wire (e.g. using Fiddler), you'll see an actual Hex B4 character is being sent from client to server in the URL. This is an illegal character in a URL, since URLs are limited to char codes under 0x80 (any larger-than-0x80 char codes must be percent-escaped).

所以您的客户端传入了一个无效字符,您的服务器正在(正确地)用 %EF%BF%BD 替换假字符,这是 Unicode 替换字符 (U+0FFD),这是遇到没有等效字符的字符时会发生的情况在本地编码中.

So your client is passing in an invalid character, and your server is (correctly) replacing the bogus character with %EF%BF%BD which is the UTF-8 encoding for the Unicode Replacement Character (U+0FFD), which is what happens when a character is encountered which has no equivalent in the local encoding.

AFAIK,这是 IE 中的错误.如果您在 Firefox 中输入相同的 URL,Firefox 将正确编码 URL(作为 %b4 而不是 ´).请注意,同样是 AFAIK,只有在手动将无效字符粘贴到 IE 的地址栏中时才会出现问题——如果链接中存在相同的字符,则 IE 似乎正确编码了 URL(至少在我测试的情况下).

AFAIK, this is a bug in IE. If you type the same URL into Firefox, Firefox will encode the URL properly (as %b4 instead of ´). Note that, also AFAIK, the problem only happens when manually pasting invalid characters into IE's address bar-- if the same character is present in a link, IE seems to encode the URL properly (at least in the cases I tested).

所以你应该弄清楚是谁向你发送了这个虚假 URL,并告诉他们开始正确编码他们的 URL!

So you should figure out who is sending this bogus URL to you, and tell them to start encoding their URLs properly!

这篇关于当查询字符串是 URLEncoded 或在 ASP.NET 中具有百分比字符时如何获取查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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