HTTP HTTP request timeout not Cancel on continuous streaming site

Region

I developed Wrapper for the Http Web Request class for more convenient execution of Gets and Posts. This library has been used without any problems for more than two years, but today we have a strange problem.

There’s one website (it will take some time to load), which, apparently, saves streaming characters in the HTML interface, so our library “Get” the request gets stuck in a loop.

Various timeout properties

Looking for a link to the Http Web Request, we find that there are two different timeout properties. We use both of them, but none of them seem to cancel properly.

  • Read Write Timeout - Gets or sets the timeout in milliseconds when writing or reading from a stream.
  • Timeout - Gets or sets the timeout value in milliseconds for the GetResponse and GetRequestStream methods.

One timeout for their proper management

Is there a way to set an “Operation Timeout” that will cause the thread / connection to be deleted after some pre-configured time?

Code example

 public class Program { public static void Main() { string url = "http://anus.io"; Console.WriteLine("Initializing Request State Object"); RequestState myRequestState = new RequestState(); // Creating Http Request myRequestState.request = (HttpWebRequest)WebRequest.Create(url); myRequestState.request.Method = "GET"; myRequestState.request.ReadWriteTimeout = 4000; myRequestState.request.Timeout = 4000; Console.WriteLine("Begining Async Request"); IAsyncResult ar = myRequestState.request.BeginGetResponse(new AsyncCallback(ResponseCallback), myRequestState); Console.WriteLine("Waiting for Results"); ar.AsyncWaitHandle.WaitOne(); myRequestState.response = (HttpWebResponse)myRequestState.request.EndGetResponse(ar); Console.WriteLine("Response status code = {0}", myRequestState.response.StatusCode); } public static void ResponseCallback (IAsyncResult asyncResult) { Console.WriteLine("Completed"); } } 

This code works fine , but how do I read the response as a string, as it is constantly being transmitted (as the browser shows)?

Question

How can these sites be handled? (By pen, I mean the ability to identify and skip situations when the server simply does not stop transmitting?)

+5
source share
2 answers

Try the following:

 public static void Main() { string url = "http://anus.io"; ***int DefaultTimeOut = 15000;*** //timeout after 15 sec Console.WriteLine("Initializing Request State Object"); RequestState myRequestState = new RequestState(); // Creating Http Request myRequestState.request = (HttpWebRequest)WebRequest.Create(url); myRequestState.request.Method = "GET"; myRequestState.request.ReadWriteTimeout = 4000; myRequestState.request.Timeout = 4000; Console.WriteLine("Begining Async Request"); IAsyncResult ar = myRequestState.request.BeginGetResponse(new AsyncCallback(ResponseCallback), myRequestState); ***ThreadPool.RegisterWaitForSingleObject(ar.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), myRequestState.request, DefaultTimeOut, true);*** Console.WriteLine("Waiting for Results"); ar.AsyncWaitHandle.WaitOne(); try{ myRequestState.response = (HttpWebResponse)myRequestState.request.EndGetResponse(ar); Console.WriteLine("Response status code = {0}", myRequestState.response.StatusCode); }catch(Exception){ Console.WriteLine("Request Aborted"); } } public static void ResponseCallback (IAsyncResult asyncResult) { Console.WriteLine("Completed"); } //call the timeout if more than 15 seconds **public static void TimeoutCallback(object state, bool timedOut) { if (timedOut) { HttpWebRequest request = state as HttpWebRequest; if (request != null) { request.Abort(); } } }** 
+1
source

I would move all the HTTP processing to Task and use the general method to time out this task. Many such methods are available.

I do not understand why you need so many individual timeouts. Can't you just say: if this request takes more than 4 seconds, it doesn’t matter, kill it.

The APM pattern and event expectation are red flags at the age of .NET 4.5 and C # 5. All this trash hides the intent of your code. Storage goes away if you use await to use the async IO provided by the WebRequest , and use the general timeout method wrapped around it.

0
source

Source: https://habr.com/ru/post/1210274/


All Articles