0

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?

3
  • 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 Commented Dec 11, 2010 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. Commented Dec 11, 2010 at 21:33
  • 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. Commented Dec 11, 2010 at 21:50

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
0

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.

Comments

0

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.