您将需要创建一个线程并将数据流式传输以进行响应。使用这样的东西:
在你的主线程中:
while (Listening)
{
// wait for next incoming request
var result = listener.BeginGetContext(ListenerCallback, listener);
result.AsyncWaitHandle.WaitOne();
}
在你班上的某个地方:
public static void ListenerCallback(IAsyncResult result)
{
var listenerClosure = (HttpListener)result.AsyncState;
var contextClosure = listenerClosure.EndGetContext(result);
// do not process request on the dispatcher thread, schedule it on ThreadPool
// otherwise you will prevent other incoming requests from being dispatched
ThreadPool.QueueUserWorkItem(
ctx =>
{
var response = (HttpListenerResponse)ctx;
using (var stream = ... )
{
stream.CopyTo(response.ResponseStream);
}
response.Close();
}, contextClosure.Response);
}