Prevent C# from encoding brackets in query string

2019-08-25 08:54发布

问题:

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?

回答1:

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



回答2:

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.



回答3:

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



回答4:

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.



回答5:

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 :)