I cannot find any documentation about how the HttpListener handles KeepAlive.
When looking at HttpListenerRequest from the HttpListenercontext I don't see any "GetRequest" methods, but only a RequestStream.
Is it save to assume that HttpListener handles KeepAlive under the hood and creates a new Context each time? If it does reuse the Context, how do I handle multiple request exactly?
I tried a simple but complete example using internet explorer (keep-alive is in the request). This creates a new context each time, but I'm not sure if this is because I'm disposing the stream.
using System.IO;
using System.Net;
namespace Sandbox
{
public class Program
{
private static void Main()
{
var listener = new HttpListener();
// might need to execute elevated cmd: netsh add urlacl url=http://localhost:6302/ user=DOMAIN\user listen=yes
listener.Prefixes.Add("http://localhost:6302/");
listener.Start();
var count = 0;
while (true)
{
var context = listener.GetContext();
try
{
// Is this dispose the reason i get a new context each time?
using (var writer = new StreamWriter(context.Response.OutputStream))
writer.Write("Hello World! {0}", ++count);
}
finally
{
// context.Response.Close();
}
}
}
}
}