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?
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
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.
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
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.
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 :)
精彩评论