防止 C# 对查询字符串中的括号进行编码

发布于 2024-10-07 19:24:50 字数 992 浏览 0 评论 0原文

我需要使用一个带有名为“names[]”的 GET 参数的服务。

例如: GET http://example.com/name2id?names[]=john< /code>

当我尝试使用 WebClient 在 C# 中使用它时,括号被编码为 %5B%5D,而服务无法理解。当我使用浏览器(未编码)发送上述内容时,一切正常。

这是不起作用的示例:

using (var client = new System.Net.WebClient())
{
    response = client.DownloadString(new Uri("http://example.com/name2id?names[]=john"));
}

由 fiddler 监控,请求如下:

GET http://example.com/name2id?names%5B%5D=john HTTP/1.1
Host: example.com
Connection: Keep-Alive

有没有办法使框架不对 URL 进行编码,或者通过其他方式解决此问题?

PS 我不控制 API,所以我无法更改它。

更新:

这有点奇怪。我找到了一个解决方案,将我的 C# 代码更改为:

using (var client = new System.Net.WebClient())
{
    response = client.DownloadString(new Uri("http://example.com/name2id?names[]=john", true));
}

注意 Uri 中的布尔 dontEscape 。它实际上已被弃用,但它有效吗?谁能解释一下吗?

I need to consume a service that takes a GET parameter named "names[]".

For example: GET http://example.com/name2id?names[]=john

When I try to consume that in C# using a WebClient, the brackets gets encoded to %5B%5D, which the servies does not understand. When I send the above using my browser (un-encoded), everything works fine.

Heres the example that does not work:

using (var client = new System.Net.WebClient())
{
    response = client.DownloadString(new Uri("http://example.com/name2id?names[]=john"));
}

Monitored by fiddler, heres the request:

GET http://example.com/name2id?names%5B%5D=john HTTP/1.1
Host: example.com
Connection: Keep-Alive

Is there any way to make the framework NOT encode the URL, or some other way around this issue?

P.S. I do not control the API so I cannot change that.

UPDATE:

This is kinda wierd. I found a solution, changing my C# code to:

using (var client = new System.Net.WebClient())
{
    response = client.DownloadString(new Uri("http://example.com/name2id?names[]=john", true));
}

Note the boolean dontEscape in Uri. It is actually deprecated, but it works? Can anyone explain this?

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

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

发布评论

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

评论(5

洛阳烟雨空心柳 2024-10-14 19:24:50

据我了解,RFC2396 [] 不是 URI 中的有效字符。所以在我看来,您的服务需要一个格式错误的 http 请求。

query         = *uric
uric          = reserved | unreserved | escaped
unreserved    = alphanum | mark
mark          = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
reserved      = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
escaped       = "%" hex hex

http://www.ietf.org/rfc/rfc2396.txt

As I understand RFC2396 [] are not valid characters in a URI. So it looks to me like your service expects a malformed http request.

query         = *uric
uric          = reserved | unreserved | escaped
unreserved    = alphanum | mark
mark          = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
reserved      = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
escaped       = "%" hex hex

http://www.ietf.org/rfc/rfc2396.txt

一袭水袖舞倾城 2024-10-14 19:24:50

您确定编码字符是真正的问题吗?

WebClient 类对字符进行了正确编码。如果服务无法处理正确编码的字符,则服务就会损坏。

如果服务以这种方式损坏,而您无论如何都必须使用它,那么您必须找到发送请求的不同方式。

Are you sure that the encoded characters is the real problem?

The characters are encoded correctly by the WebClient class. If the service can't handle characters that are correctly encoded, then the service is broken.

If the service is broken in that way, and you have to use it anyway, then you have to find a different way of sending the request.

拥有 2024-10-14 19:24:50

从技术上讲,将 [] url 编码为 %5B%5D 是正确的,因此问题不在于使用 [],而是提供程序未正确对路径/查询字符串进行 url 解码

http://www.albionresearch.com/misc/urlencode.php

techincally its correct to url encode [] to %5B%5D, so the problem is not using [] but that the provider doesn't correctly url decode the path/querystring

http://www.albionresearch.com/misc/urlencode.php

给不了的爱 2024-10-14 19:24:50

您应该能够将 URI 作为字符串传递,

所以这样做

 response = client.DownloadString("http://example.com/name2id?names[]=john");

应该可以在不编码 url 的情况下工作。

不过还没有尝试过,所以我可能是错的。

You should just be able to pass the URI as a string

So doing

 response = client.DownloadString("http://example.com/name2id?names[]=john");

Should work without encoding the url.

Haven't tried it though so i might be wrong.

安人多梦 2024-10-14 19:24:50

正如 CodeInChaos 提到的,根据 RFC2396,该 URL 无效。

为了使 C# 不转义无效字符,可以使用 URI 的重载方法:

new Uri("http://example.com/name2id?names[]=john", true)

但是,此方法已被弃用,但显然它仍然有效。我必须忍受弹出的警告:)

As mentioned by CodeInChaos, the URL is not valid according to RFC2396.

To make C# not escape invalid characters, the overloaded method of URI can be usen:

new Uri("http://example.com/name2id?names[]=john", true)

However, this method is deprecated but apparently it still works. I have to live with the warning popping up :)

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