httpgetrequest uri encoded to iso-8859-2

944 views Asked by At

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.

1

There are 1 answers

0
nzrp On

About percent-encoding

The problem here is

... URI schemes ... should convert all other characters to bytes according to UTF-8, and then percent-encode those values.

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 using HttpWebRequest.Create

Here is my example. First I wrote simple function that does binary %-encoding:

private string URLEncodeBin(byte[] bytes)
{
    string s = "";
    foreach (byte b in bytes) // encoding every byte
        s += "%" + b.ToString("X2"); // format - 2 digits HEX
    return s;
}

And then I used it like this

// creating %-encoded data from iso-8859-2 string
System.Text.Encoding iso_8859_2 = System.Text.Encoding.GetEncoding("iso-8859-2");
byte[] bytes = iso_8859_2.GetBytes("ĄĽŚŠŤŹŽąľśˇšťźž"); // for example
string data=URLEncodeBin(bytes);

// forming request
this._request = "a=" + data;

String myURI = (this._services + "?" + this._request);
HttpWebRequest request = HttpWebRequest.Create(myURI) as HttpWebRequest;
request.Method = "GET";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
this._respStream = response.GetResponseStream();

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.