Is it possible to call HttpListener.GetContext with a timeout?

According to the HttpListener link , the call to HttpListener.GetContext will be blocked until it receives an HTTP request from the client.

I wonder if I can specify a timeout so that the function returns after the time expires. I think that otherwise it is unreasonable, since you cannot guarantee that there will be a request to return this function, then how can this call be completed?

PS I know that there is an asynchronous version (BeginGetContext), but the problem remains, because the corresponding EndGetContext will be blocked until the HTTP request arrives .

This way, there will always be one thread (if you do multithreading) it will not be able to return, because it is blocked while waiting for a request.

Did I miss something?

UPDATE:

I found this link to be useful. I also found that calling HttpListener.Close () actually terminates the waiting threads created by BeginGetContext () s. Somehow, HttpListener.Close () starts the callbacks registered by BeginGetContext (). So, before you execute HttpListener.EndGetContext (), check if the HttpListener is stopped.

+8
source share
2 answers

The callback that EndGetContext calls should never be called unless the HTTP request has already been received or the listener has failed (in which case EndGetContext throws an exception). Thus, it will not block if used properly.

BeginGetContext and EndGetContext - that is, asynchronous operations - is what you want.

Begin and end the methods that Begin says, โ€œsignal me when X is ready,โ€ and End says, โ€œGive me the X that you just told me about.โ€ Naturally, the latter will be blocked theoretically, but will instantly return.

+2
source

In addition, if you want line-by-line processing for a limited time, BeginGetContext returns System.IAsyncResult displaying the AsyncWaitHandle property

var context = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); context.AsyncWaitHandle.WaitOne(); 

Above, it blocks the stream until the listener receives something real, as defined by the headers assigned to the listener, or ends due to some exception that terminates the listener stream and returns the result back to the ListenerCallback.

But AsyncWaitHandle.WaitOne () can accept timeout parameters

 // 5 seconds timeout bool success = context.AsyncWaitHandle.WaitOne(5000, true); if (success == false) { throw new Exception("Timeout waiting for http request."); } 

ListenerCallback may contain a call to listener.EndGetContext or just call listener.EndGetContext in a string if there is no timeout or error specified in AsyncWaitHandle

 public static void ListenerCallback(IAsyncResult result) { HttpListener listener = (HttpListener) result.AsyncState; // Use EndGetContext to complete the asynchronous operation. HttpListenerContext context = listener.EndGetContext(result); HttpListenerRequest request = context.Request; // Get response object. HttpListenerResponse response = context.Response; // Construct a response. string responseString = "<HTML><BODY> It Works!</BODY></HTML>"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Write to response stream. response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer,0,buffer.Length); // Close the output stream. output.Close(); } 

Remember to listen again using listener.BeginGetContext

+4
source

All Articles