Can I use Unicode to decode an HTTP request?

As you said the default encoding of an HTTP POST request is ISO-8859-1. Otherwise you have to look at the Content-Type header that might then look like Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Once you have read the posted data into a byte array you may decide to convert this buffer to a string (remember all strings in . NET are UTF-16).

It is only at that moment that you need to know the encoding byte buffer = ReadFromRequestStream(...) string data = Encoding . GetEncoding("DETECTED ENCODING OR ISO-8859-1") . GetString(buffer) And to answer your question: Am I able to use Unicode to decode an HTTP request given as a byte array?

Yes, if unicode has been used to encode this byte array: string data = Encoding. UTF8. GetString(buffer).

As you said the default encoding of an HTTP POST request is ISO-8859-1. Otherwise you have to look at the Content-Type header that might then look like Content-Type: application/x-www-form-urlencoded; charset=UTF-8. Once you have read the posted data into a byte array you may decide to convert this buffer to a string (remember all strings in .

NET are UTF-16). It is only at that moment that you need to know the encoding. Byte buffer = ReadFromRequestStream(...) string data = Encoding .

GetEncoding("DETECTED ENCODING OR ISO-8859-1") . GetString(buffer); And to answer your question: Am I able to use Unicode to decode an HTTP request given as a byte array? Yes, if unicode has been used to encode this byte array: string data = Encoding.

UTF8. GetString(buffer).

You don't use a unicode encoding to decode something that is not encoded using a unicode encoding, as that would not correctly decode all characters. Create an Encoding object for the correct encoding and use that: Encoding iso = Encoding. GetEncoding("iso-8859-1"); string request = iso.

GetString(requestArray).

The code given below should help, if you are expecting large amount of data streaming down then doing it asynchronously is the best way to go about. String myUrl = @"somedomain.com/file"; HttpWebRequest request = (HttpWebRequest)HttpWebRequest. Create(myUrl); //Set some reasonable limits on resources used by this request request.

MaximumAutomaticRedirections = 4; request. MaximumResponseHeadersLength = 4; request. Timeout = 15000; response = (HttpWebResponse)request.GetResponse(); Stream receiveStream = response.

GetResponseStream(); Encoding encode = System.Text.Encoding. GetEncoding("utf-8"); StreamReader readStream = new StreamReader(receiveStream, encode); Char read = new Char512; // Reads 512 characters at a time. Int count = readStream.

Read(read, 0, 512); while (count > 0) { // Dumps the 512 characters on a string and displays the string. String str = new String(read, 0, count); count = readStream. Read(read, 0, 512); }.

Sorry, that looks like code to request from a web resource and decode the response. I guess I should clarify my question. You're doing something with UTF-8 there... Can I just use that to decode an HTTP request?

– Charlie Somerville May 9 '09 at 11:01.

Every time . NET transfers information between an external representation (e.g. A TCP socket) and the internal Unicode format (or the other way around), some form of encoding is involved. See utf-8-vs-unicode, especially Jon Skeet's answer, with the reference to Joel's article The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!

).

Joel's article was the very reason I decided to think about Encoding rather than just blindly using ASCII :p – Charlie Somerville May 9 '09 at 23:32 Remember, UTF-8 or even UTF-16 is not Unicode. – gimel May 10 '09 at 5:00.

HttpWebRequest request = (HttpWebRequest)HttpWebRequest. Stream receiveStream = response. Encoding encode = System.Text.Encoding.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions