I saw this question often asked, however I couldn't find solution. Why is the request streal always null. This is the code:
static void Main(string[] args)
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:1330/");
listener.Start();
while (true)
{
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
request.InputStream.Position = 0;
Console.WriteLine(request.InputStream.Length);
}
}
The length is 0 and I tried reading it with stream reader and in a byte array:
request.InputStream.Position = 0;
StreamReader reader = new StreamReader(request.InputStream);
string test = reader.ReadToEnd();
Console.WriteLine(test);
Or
request.InputStream.Position = 0;
byte[] buffer = new byte[10000];
request.InputStream.Read(buffer, 0, buffer.Length);
Console.WriteLine(Encoding.UTF8.GetString(buffer));
Both are empty - they just give 7 or 8 ("\n") newlines, I say that because I noticed the console cursor jumps 7 lines below, when a clients connects, without writing any non-blank string.