private void ListenerCallback(IAsyncResult ar)
{
_busy.WaitOne();
try
{
HttpListenerContext context;
try
{ context = _listener.EndGetContext(ar); }
catch (HttpListenerException)
{ return; }
if (_stop.WaitOne(0, false))
return;
var sr = new StreamReader(context.Request.InputStream);
string x = sr.ReadToEnd();
Console.WriteLine("{0} {1}", context.Request.HttpMethod, x);
//context.Response.SendChunked = true;
using (TextWriter tw = new StreamWriter(context.Response.OutputStream))
{
//for (int i = 0; i < 5; i++)
{
//tw.WriteLine("<p>{0} @ {1}</p>", i, DateTime.Now);
tw.WriteLine("<html><head></head><body>");
tw.WriteLine("Server Response");
tw.WriteLine("</body></html>");
tw.Flush(); //Catch http exception if client exists halfway through
//Thread.Sleep(1000);
}
}
}
finally
{
if (_maxThreads == 1 + _busy.Release())
_idle.Set();
}
}
以上是我的代码,我可以使用 Chrome 访问 URL,即使服务器显示它需要 2 个获取请求,我也可以很少回复,我希望能够处理 POST 请求,当我发送一个发布请求时它会正确读取它,但是客户没有得到答复。