Uri 类 (.NET) 中的奇怪行为
为什么 Uri 类 url 会对我发送给其构造函数的 url 进行解码,如何防止这种情况发生?
示例(查看查询字符串值“选项”):
string url = "http://www.example.com/default.aspx?id=1&name=andreas&options=one%3d1%26two%3d2%26three%3d3";
Uri uri = new Uri(url); // http://www.example.com/default.aspx?id=1&name=andreas&options=one=1&two=2&three=3
更新:
// ?id=1&name=andreas&options=one%3d1%26two%3d2%26three%3d3
Request.QueryString["options"] = one=1&two=2&three=3
// ?id=1&name=andreas&options=one=1&two=2&three=3
Request.QueryString["options"] = one=1
这是我的问题:)
Why does the Uri class urldecode my url that I send to its contructor and how can I prevent this?
Example (look at the querystring value "options"):
string url = "http://www.example.com/default.aspx?id=1&name=andreas&options=one%3d1%26two%3d2%26three%3d3";
Uri uri = new Uri(url); // http://www.example.com/default.aspx?id=1&name=andreas&options=one=1&two=2&three=3
Update:
// ?id=1&name=andreas&options=one%3d1%26two%3d2%26three%3d3
Request.QueryString["options"] = one=1&two=2&three=3
// ?id=1&name=andreas&options=one=1&two=2&three=3
Request.QueryString["options"] = one=1
This is my problem :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
究竟为什么?
获取编码版本
您可以使用
url.AbsoluteUri
EDITwhy exactly?
you can get to the encoded version using
url.AbsoluteUri
EDIT
我希望 Uri 类能够做到这一点。我非常确定,如果您将它与 WebClient 类(即 WebClient.OpenRead (Uri uri))一起使用,它仍然会让您处于一个好的位置。你的情况有什么问题吗?
I would expect that from a Uri class. I am quite sure that it still gets you in a good place if you use it with e.g. WebClient class (i.e. WebClient.OpenRead (Uri uri)). What's the problem in your case?
这就是 .NET 内部代码的行为方式 - 在以前的版本中,您可以使用
Uri
的另一个构造函数,该构造函数接受告诉是否转义的布尔值,但它已被弃用。解决这个问题的唯一方法是 hackish:通过反射直接访问一些私有方法:
这在快速测试中对我有用,但当你“破解”.NET 内部代码时并不理想。
This is how the internal code of .NET behaves - in previous versions you could use another constructor of
Uri
that accepted boolean value telling if to escape or not, but it has been deprecated.The only way around it is hackish: accessing some private method directly by means of reflection:
This worked for me in quick test, but not ideal as you "hack" into .NET internal code.