Hello I spent ages by searching community forums to find working solution. It looks simple job, I need send HTTP request passing arguments by GET method, but server side service required URI encoded in ISO-8895-2. I use .NET System.net class HttpWebRequest. My code:
String myURI = (this._services + "?" + this._request);
HttpWebRequest request = HttpWebRequest.Create(myURI) as HttpWebRequest;
request.ContentType = "application/x-www-form-urlencoded, charset=iso-8859-2";
request.Method = "GET";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
this._respStream = response.GetResponseStream();
Now question is how to encode string myURI to ISO-8859-2. If I do whatever, string is represented like utf-8. I tried a couple of conversion described in this forum, see example:
System.Text.Encoding iso_8859_2 = System.Text.Encoding.GetEncoding("iso-8859-2");
byte[] iso592 = iso_8859_2.GetBytes(myURI);
String isoURI = iso_8859_2.GetString(iso592, 0, iso592.Length);
Sequence of bytes iso592 is always same, even when I used utf-8 or windows-1250 encoding. I have read some articles that .NET string is always represent like unicode. So what to do now. Is there any way how to create WebRequest instance with ISO encoded URI ? When I switch to POST method and then streaming data to HTTP header like this:
Encoding iso_8859_2 = System.Text.Encoding.GetEncoding("iso-8859-2");
byte[] isoData = iso_8859_2.GetBytes(getUrl);
HttpWebRequest request = HttpWebRequest.Create(this._services) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded; charset=iso-8859-2";
request.ContentLength = isoData.Length;
Stream header = request.GetRequestStream();
header.Write(isoData, 0, isoData.Length);
header.Close();
everything works fine, but I have to use GET method.
About percent-encoding
The problem here is
So you can't use standard methods to achieve the goal. The method in question is
HttpWebRequest.Create(myURI)
, that encodes request and makes it UTF-8 (and %-encoded). To avoid that you need to manually %-encode your data in request like binary data before usingHttpWebRequest.Create
Here is my example. First I wrote simple function that does binary %-encoding:
And then I used it like this
You should encode only parameters, not all request. And
request.ContentType = "application/x-www-form-urlencoded, charset=iso-8859-2";
is pointless, because you dont have content, only URL.The idea is absolutely platform independent, I don't know why no one have figured it out already.