简体   繁体   English

C#HttpClient URI不能正确转义

[英]C# HttpClient URI is not escaping properly

I'm trying to get my URL to escape but it's not working properly. 我试图让我的网址转义,但无法正常工作。 Ironically, on my MacBook when I execute this part of code 具有讽刺意味的是,在我的MacBook上,当我执行这部分代码时

Uri url = new Uri("http://www.example.com/?i=123%34", true);
// it returns http://www.example.com/?i=123%34 which is exactly what I want.

The problem is that my IDE says it's obsolete and it does not work on my Windows machine. 问题是我的IDE表示它已过时并且在Windows计算机上不起作用。 It's the exact same project, and IDE. 这是和IDE完全相同的项目。 So I tried to find a solution, which someone suggested 所以我试图找到一个解决方案,有人建议

Uri uri = new Uri(Uri.EscapeUriString("http://www.example.com/?i=123%34")); 
// this returns http://www.example.com/?i=123%2534 which is what I DONT want.

So how do I approach this issue? 那么我该如何解决这个问题呢? I looked all over the web and I can't find any solutions. 我浏览了整个网络,找不到任何解决方案。 I need to know how to properly escape this URL. 我需要知道如何正确转义此URL。 The second method posted above does not work like the first method above. 上面发布的第二种方法与上面的第一种方法不同。

I verified the GET requests via Fiddler, so everything is indeed happening. 我通过Fiddler验证了GET请求,因此确实发生了所有事情。

Update: 更新:

Again, I need the server to receive the URL exactly how the string is declared. 同样,我需要服务器接收URL的确切字符串声明方式。 I want the server to handle the conversion. 我希望服务器处理转换。 I cannot substitute %25 for the % symbol. 我不能用%25代替%符号。 It MUST be received exactly how I the string is declared. 必须完全按照我声明字符串的方式接收它。 Additionally, " http://www.example.com/?i=1234 " is NOT what I want either. 此外,“ http://www.example.com/?i=1234 ”也不是我想要的。

如果您对字符串ht tp://www.example.com/?i = 1234表示Uri url = new Uri(Uri.UnescapeDataString("http://www.example.com/?i=123%34")); ,可以尝试Uri url = new Uri(Uri.UnescapeDataString("http://www.example.com/?i=123%34"));

The problem is with the configuration of your web server on Windows, that allows double escaping. 问题在于Windows上的Web服务器的配置,该配置允许两次转义。 Your original URL is http://www.example.com/?i=123%34 , which when unescaped, becomes http://www.example.com/?i=1234 . 您的原始URL是http://www.example.com/?i=123%34 ,如果未转义,则变为http://www.example.com/?i=1234

Your web server on Windows, on the other hand, escapes the % character again instead of unescaping %34 . 另一方面,Windows上的Web服务器再次转义%字符,而不是转义%34 Thus, it turns into http://www.example.com/?i=123%2534 . 因此,它变成http://www.example.com/?i=123%2534

This is why you should not use characters like % in the URL before it gets escaped. 这就是为什么在转义符之前,不应在URL中使用诸如%之类的字符。

Edit - 编辑-

I typed the following two URLs in Firefox to see how the parameters are received on the server. 我在Firefox中键入了以下两个URL,以查看如何在服务器上接收参数。

The value of i in http://www.example.com/?i=123%34 is 1234 . http://www.example.com/?i=123%34中i值为1234

The value of i in http://www.example.com/?i=123%2534 is 123%34 http://www.example.com/?i=123%2534中i值为123%34

If the server must receive the % character, it must be escaped in order for it to be dispatched over HTTP. 如果服务器必须接收%字符,则必须对其进行转义以通过HTTP进行分派。 There's literally no other way to send it over the wire. 实际上,没有其他方法可以通过电线发送它。 If you don't escape the % character, it will be treated as an escape sequence along with 34 and automatically turn into 4 on the server. 如果不对%字符进行转义,它将与34一起作为转义序列,并在服务器上自动变为4

If your network inspector shows you unescaped text in the request, it's because it's prettifying the URL before displaying it to you. 如果您的网络检查员在请求中显示了未转义的文本,那是因为在向您显示URL之前,它会美化URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM