Network outage management

I am trying to do a "long polling" with an HttpWebRequest object.

In my C # application, I am making an HTTP GET request using HttpWebRequest. And then, then, I'm waiting for an answer with beginGetResponse (). I use ThreadPool.RegisterWaitForSingleObject to wait for a response or timeout (after 1 minute).

I set the target web server for a long time to respond. Thus, I have time to disconnect the network cable.

After sending the request, I pull out the network cable.

Is there any way to get an exception when this happens? So I do not need to wait for a timeout?

Instead of an exception, a timeout (from RegisterWaitForSingleObject) occurs after a timeout of 1 minute has elapsed.

Is there a way to determine if the network connection is down? Currently, this situation is indistinguishable from the case when the web server takes more than 1 minute to respond.

+4
source share
3 answers

I found a solution:

Before calling beginGetResponse, I can call the following in an HttpWebRequest:

req.ServicePoint.SetTcpKeepAlive( true, 10000, 1000) 

I think this means that after 10 seconds of inactivity, the client will send TCP "keep alive" to the server. This will fail if the network connection is disconnected because the network cable was pulled out.

So, when the cable is pulled, I continue to receive messages for 10 seconds (maximum), and then a callback for BeginGetResponse occurs. In the callback, I get an exception when I call req.EndGetResponse ().

I guess this wins one of the benefits of a lengthy survey. Since we are still sending packages.

+8
source

I will leave it to you to try to pull the plug of it.

 ManualResetEvent done = new ManualResetEvent(false); void Main() { // set physical address of network adapter to monitor operational status string physicalAddress = "00215A6B4D0F"; // create web request var request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://stackoverflow.com")); // create timer to cancel operation on loss of network var timer = new System.Threading.Timer((s) => { NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces() .FirstOrDefault(nic => nic.GetPhysicalAddress().ToString() == physicalAddress); if(networkInterface == null) { throw new Exception("Could not find network interface with phisical address " + physicalAddress + "."); } else if(networkInterface.OperationalStatus != OperationalStatus.Up) { Console.WriteLine ("Network is down, aborting."); request.Abort(); done.Set(); } else { Console.WriteLine ("Network is still up."); } }, null, 100, 100); // start asynchronous request IAsyncResult asynchResult = request.BeginGetResponse(new AsyncCallback((o) => { try { var response = (HttpWebResponse)request.EndGetResponse((IAsyncResult)o); var reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8); var writer = new StringWriter(); writer.Write(reader.ReadToEnd()); Console.Write(writer.ToString()); } finally { done.Set(); } }), null); // wait for the end done.WaitOne(); } 
+3
source

I don’t think you will like it. You can check your Internet connection after making a request to a slow server.

There are many ways to do this - from another request to google.com (or some IP address on your network) to P / Invoke. You can get more information here: The fastest way to test your internet connection

After you create the original request, you will go into a cycle that checks the Internet connection and until the Internet works and the original request returns (it can set a variable to stop the cycle).

Does it help at all?

+2
source

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


All Articles