System.Uri 查询字符串中的文字与符号

发布于 2024-09-02 14:13:15 字数 1203 浏览 5 评论 0原文

我正在开发一个客户端应用程序,它使用宁静的服务来按名称查找公司。 重要的是,我能够在查询中包含文字与符号,因为该字符在公司名称中很常见。

但是,每当我将 %26(URI 转义的 & 符号)传递给 System.Uri 时,它都会将其转换回常规 & 符号!仔细检查后发现,唯一未转换回来的两个字符是哈希值 (%23) 和百分比 (%25)。

假设我想搜索一家名为“Pierce & Pierce”的公司:

var endPoint = "http://localhost/companies?where=Name eq '{0}'";
var name = "Pierce & Pierce";
Console.WriteLine(new Uri(string.Format(endPoint, name)));
Console.WriteLine(new Uri(string.Format(endPoint, name.Replace("&", "%26"))));
Console.WriteLine(new Uri(string.Format(endPoint, Uri.EscapeUriString(name))));
Console.WriteLine(new Uri(string.Format(endPoint, Uri.EscapeDataString(name))));

上述所有四个组合都会返回:

http://localhost/companies?where=Name eq 'Pierce & Pierce'

这会导致服务器端出现错误,因为&符号(正确地)被解释为查询参数分隔符。我真正需要它返回的是原始字符串:

http://localhost/companies?where=Name eq 'Pierce %26 Pierce'

如何在不完全丢弃 System.Uri 的情况下解决此行为? 我无法在最后一刻用 %26 替换所有 & 符号,因为通常会涉及多个查询参数,而且我不想破坏它们的分隔符。

注意:此问题中讨论了类似的问题,但是我特别指的是System.Uri

I'm working on a client app that uses a restful service to look up companies by name.
It's important that I'm able to include literal ampersands in my queries since this character is quite common in company names.

However whenever I pass %26 (the URI escaped ampersand character) to System.Uri, it converts it back to a regular ampersand character! On closer inspection, the only two characters that aren't converted back are hash (%23) and percent (%25).

Lets say I want to search for a company named "Pierce & Pierce":

var endPoint = "http://localhost/companies?where=Name eq '{0}'";
var name = "Pierce & Pierce";
Console.WriteLine(new Uri(string.Format(endPoint, name)));
Console.WriteLine(new Uri(string.Format(endPoint, name.Replace("&", "%26"))));
Console.WriteLine(new Uri(string.Format(endPoint, Uri.EscapeUriString(name))));
Console.WriteLine(new Uri(string.Format(endPoint, Uri.EscapeDataString(name))));

All four of the above combinations return:

http://localhost/companies?where=Name eq 'Pierce & Pierce'

This causes errors on the server side since the ampersand is (correctly) interpreted as a query arg delimiter. What I really need it to return is the original string:

http://localhost/companies?where=Name eq 'Pierce %26 Pierce'

How can I work around this behavior without discarding System.Uri entirely?
I can't replace all ampersands with %26 at the last moment because there will usually be multiple query args involved and I don't want to destroy their delimiters.

Note: A similar problem was discussed in this question but I'm specifically referring to System.Uri.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里梦着梦中梦 2024-09-09 14:13:15

URL 中不正确的不仅仅是“&”符号。有效的 URL 不能包含空格。

EscapeDataString 方法可以很好地对字符串进行正确编码,您应该对整个值进行编码,而不仅仅是名称:

Uri.EscapeDataString("Name eq 'Pierce & Pierce'")

结果:

Name%20eq%20'Pierce%20%26%20Pierce'

当您使用此字符串创建 Uri 时,它会是正确的。要查看 URL,您可以使用 AbsoluteUri 属性。如果您只是将 Uri 转换为字符串(调用 ToString 方法),则 URL 将不会转义,因此显示不正确。

It's not only the ampersand that is incorrect in the URL. A valid URL can not contain spaces.

The EscapeDataString method works just fine to encode the string properly, and you should encode the entire value, not just the name:

Uri.EscapeDataString("Name eq 'Pierce & Pierce'")

Result:

Name%20eq%20'Pierce%20%26%20Pierce'

When you create an Uri using this string, it will be correct. To see the URL you can use the AbsoluteUri property. If you just convert the Uri to a string (which calls the ToString method) the URL will be unescaped and thus appear incorrect.

甜警司 2024-09-09 14:13:15

我遇到了同样的问题。虽然查询字符串使用 Uri.EscapeDataString 进行转义,并且属性 AbsoluteUri 确实正确地表示它,但 WebBrowser 以未转义的格式发送 Uri。

  currentUri = new System.Uri(ServerAgent.urlBase + "/MailRender?uid="
+ Uri.EscapeDataString(uid);

webBrowser.Navigate(currentUri);

加号 ('+') 被转换为 %2B,但服务器仍然在 URL 中获取 +,然后通过 HttpServeletRequest.getParameter() 调用将其转换为空格 (' ')

I encount the same problem. Though the query string is escaped with Uri.EscapeDataString, and the property AbsoluteUri do reprent it correctly, but WebBrowser send the Uri in unescaped format.

  currentUri = new System.Uri(ServerAgent.urlBase + "/MailRender?uid="
+ Uri.EscapeDataString(uid);

webBrowser.Navigate(currentUri);

Plus sign ('+') is convert to %2B, but the server still get + in URL, which is then convert to space (' ') via HttpServeletRequest.getParameter() call

一抹苦笑 2024-09-09 14:13:15

我通过创建 Uri 的派生类解决了这个问题。

    class Uri2 : System.Uri
{
    public Uri2(string url) : base(url)
    {
    }

    public override string ToString()
    {
        return AbsoluteUri;
    }
}

在任何使用 System.Uri 的地方,都使用 Uri2 替换。
我不知道这是否是.NetCF的错误,WebBrowser应该以编码格式发送URL,即AbsoluteUri的值,而不是ToString()的值

I have resolved this problem by create a derived class of Uri

    class Uri2 : System.Uri
{
    public Uri2(string url) : base(url)
    {
    }

    public override string ToString()
    {
        return AbsoluteUri;
    }
}

In any place System.Uri is used, use Uri2 replaced.
I don't know whether this is a bug of .NetCF, WebBrowser should send URL in encoded format, i.e. the value of AbsoluteUri, but not value from ToString()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文