I am accepting a POST request like so:
Socket connection = m_connection; Byte[] receive = new Byte[1024]; int received = connection.Receive(receive); Console.WriteLine(received.ToString()); string request = Encoding.ASCII.GetString(receive); Console.WriteLine(request);
The post values end up being weird, if I post text values a lot of times they end up with a lot of +’s behind them. If I post C:\Users\John Doe\wwwroot, it ends up being: C%3A%5CUsers%5John+Doe%5Cwwwroot
index.html becomes index.html++++++++++++++++++++++++++++++++
It seems I am getting the Encoding wrong somehow, however I tried multiple encodings, and they have same weirdness. What is the best way to correctly read a HTTP POST request from a socket byte stream?
You should use System.Web.HttpUtility.UrlDecode not Encoding.ASCII to peform the decoding.
You will probably get away with passing Encoding.Default as the second parameter to this static method.
Your are seeing the result of a HTML form POST which encodes the values as if they were being appended to a URL as a search string. Hence it is a & delimited set of name=value pairs. Any out-of-band characters are encoded to their hex value %xx.
The UrlDecode method will decode all this for you.
As other have stated you really need to chunk the stream in, it may be bigger that 1K.
Strictly speaking you should check the Content-Type header for any ;CharSet= attribute. If present you need to ensure the character encode you pass to UrlDecode is appropriate to that CharSet (e.g., if CharSet=UTF-8 then use Encoding.UTF8).