System.Uri 查询字符串中的文字与符号
我正在开发一个客户端应用程序,它使用宁静的服务来按名称查找公司。 重要的是,我能够在查询中包含文字与符号,因为该字符在公司名称中很常见。
但是,每当我将 %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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
URL 中不正确的不仅仅是“&”符号。有效的 URL 不能包含空格。
EscapeDataString
方法可以很好地对字符串进行正确编码,您应该对整个值进行编码,而不仅仅是名称:结果:
当您使用此字符串创建
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:Result:
When you create an
Uri
using this string, it will be correct. To see the URL you can use theAbsoluteUri
property. If you just convert theUri
to a string (which calls theToString
method) the URL will be unescaped and thus appear incorrect.我遇到了同样的问题。虽然查询字符串使用 Uri.EscapeDataString 进行转义,并且属性 AbsoluteUri 确实正确地表示它,但 WebBrowser 以未转义的格式发送 Uri。
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.
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
我通过创建 Uri 的派生类解决了这个问题。
在任何使用 System.Uri 的地方,都使用 Uri2 替换。
我不知道这是否是.NetCF的错误,WebBrowser应该以编码格式发送URL,即AbsoluteUri的值,而不是ToString()的值
I have resolved this problem by create a derived class of Uri
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()