Uri 类 (.NET) 中的奇怪行为

发布于 2024-12-18 21:48:33 字数 645 浏览 1 评论 0原文

为什么 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 技术交流群。

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

发布评论

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

评论(3

长伴 2024-12-25 21:48:33

究竟为什么?
获取编码版本

您可以使用 url.AbsoluteUri EDIT

Console.WriteLine("1) " + uri.AbsoluteUri);
Console.WriteLine("2) " + uri.Query);

OUT:
1) http://www.example.com/default.aspx?id=1&name=andreas&options=one%3d1%26two%3d2%26three%3d3
2) ?id=1&name=andreas&options=one%3d1%26two%3d2%26three%3d3

why exactly?
you can get to the encoded version using url.AbsoluteUri

EDIT

Console.WriteLine("1) " + uri.AbsoluteUri);
Console.WriteLine("2) " + uri.Query);

OUT:
1) http://www.example.com/default.aspx?id=1&name=andreas&options=one%3d1%26two%3d2%26three%3d3
2) ?id=1&name=andreas&options=one%3d1%26two%3d2%26three%3d3
夜司空 2024-12-25 21:48:33

我希望 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?

冬天的雪花 2024-12-25 21:48:33

这就是 .NET 内部代码的行为方式 - 在以前的版本中,您可以使用 Uri 的另一个构造函数,该构造函数接受告诉是否转义的布尔值,但它已被弃用。

解决这个问题的唯一方法是 hackish:通过反射直接访问一些私有方法:

string url = "http://www.example.com/default.aspx?id=1&name=andreas&options=one%3d1%26two%3d2%26three%3d3";
Uri uri = new Uri(url);
MethodInfo mi = uri.GetType().GetMethod("CreateThis", BindingFlags.NonPublic | BindingFlags.Instance);
if (mi != null)
    mi.Invoke(uri, new object[] { url, true, UriKind.RelativeOrAbsolute  });

这在快速测试中对我有用,但当你“破解”.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:

string url = "http://www.example.com/default.aspx?id=1&name=andreas&options=one%3d1%26two%3d2%26three%3d3";
Uri uri = new Uri(url);
MethodInfo mi = uri.GetType().GetMethod("CreateThis", BindingFlags.NonPublic | BindingFlags.Instance);
if (mi != null)
    mi.Invoke(uri, new object[] { url, true, UriKind.RelativeOrAbsolute  });

This worked for me in quick test, but not ideal as you "hack" into .NET internal code.

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