I am trying to send a request to a URL like this "http://mysite.dk/tværs?test=æ" from an asp.net application, and I am having problems with the correct encoding of the request. Or maybe the query string is encoded correctly, the service I'm connecting to just doesn't understand it correctly.
I tried sending a request with different browsers and logging how they encode the request using Wireshark, and I get the following results:
Firefox: http://mysite.dk/tv%C3%A6rs?test=%E6
Ie8: http://mysite.dk/tv%C3%A6rs?test=\xe6
Curl: http://mysite.dk/tv\xe6rs?test=\xe6
Both Firefox, IE, and Curl get the correct results from the service. Note that they encode the Danish special character "æ" differently in querystring.
When I submit a request from my asp.net application using HttpWebRequest, the URL gets the encoding as follows:
http://mysite.dk/tv%C3%A6rs?test=%C3%A6
It encodes the query string in the same way as the path to the URL. The remote service does not understand this encoding, so I do not get the correct answer.
For the entry, “æ” (U + 00E6) is% E6 in ISO-LATIN-1 and% C3% A6 in UTF-8.
I could change the remote service to accept UTF-8 encoded encoding, but then the service will stop working in browsers, and I'm not very interested. Is there a way to tell .NET that it should not encode requests with UTF-8?
I create a web request as follows:
var req = WebRequest.Create("http://mysite.dk/tværs?test=æ") as HttpWebRequest;
But the problem seems to come from System.Uri, which is apparently used internally by WebRequest.Create:
var uri = new Uri("http://mysite.dk/tværs?test=æ"); // now uri.AbsolutePath == "http://mysite.dk/tv%C3%A6rs?test=%C3%A6"