Take the 2-minute tour ×

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.


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?

share|improve this question
I think the only way around is to not use a WebClient but do a manual request using a normal socket, but even then it wouldn't really be a valid request so i'd wonder if the service would accept it –  Doggett Dec 11 '10 at 21:29
You could always ask the provide to use reasonable names in their arguments, and of course verify that they really do require "[]" in the name. This requirement seems pretty ridiculous to me. –  Jonathan Wood Dec 11 '10 at 21:33
See the updated. –  Tommy Jakobsen Dec 11 '10 at 21:45
Guess it's deprecated because it allows you to create an invalid URI. But since you need an invalid URI I'd ignore the warning. –  CodesInChaos Dec 11 '10 at 21:50

5 Answers 5

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
escaped       = "%" hex hex


share|improve this answer
That is true, but it's what the service expects, and I cannot change that. It does work through a browser, and I actually found a solution. I will update the question. –  Tommy Jakobsen Dec 11 '10 at 21:42
RFC 3986 (tools.ietf.org/html/rfc3986#section-2.2) lists brackets as reserved characters. However, "reserved" means they're only eligible for special treatment, depending on server requirements. This is a major problem with all current HTTP options in .NET in 4.5, as they all escape the URI at some point. –  McGuireV10 Jun 20 at 18:15

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.

share|improve this answer

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


share|improve this answer

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.

share|improve this answer
up vote 0 down vote accepted

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:


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

share|improve this answer

Your Answer


