Forum Xamarin Xamarin.iOS

HttpWebRequest and HttpClient.Get are both hanging, both ignore the timeout value

EdBarnesEdBarnes USMember ✭✭

I have read all the posts / links I can find on this forum and on StackOverflow but I haven't hit on the answer. This is probably a rare situation but it is absolutely crippling us.

We have a Xamarin.iOS App. The primary purpose of this app is to connect to a device that we manufacture and sell. This device has the ability to create a WIFI hotspot which does not have internet access. In other words, when the user connects to the WIFI hotspot, they have lost internet connectivity. Our typical user will connect their iPhone to the device WIFI, run their tests and will try to upload it to the internet. This won't work however as it is not connected to the internet.

This is where it gets tricky - some users are savvy enough to manage via the Settings app on their iPhones to change the settings on the WIFI connection to device which will allow the device to fail-over to their data plans. So they can upload their tests via their data plans. They do this by changing the Configure IP and Configure DNS to manual and giving them static values. So in reality we may or may not have internet access. ** We don't know and we can't tell without actually trying to connect.**

The Reachability recipe completely falls down here - if you have a network connection it assumes you have connectivity and it tries to connect to

We have written code to try as well:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(_address));
            request.Timeout = 5000;
            request.Method = "HEAD";
            request.KeepAlive = false;
            response = (HttpWebResponse)await request.GetResponseAsync(token);
            if ((response != null) && (response.StatusCode == HttpStatusCode.OK))
                _connected = true;

If we are on a network that does not have internet connectivity the GetResponseAsync will hang and it will ignore the 5 second timeout. Eventually it will come back after 1 or 2 minutes. Other times you will get a TaskScheduler_UnobservedTaskException in the App Delegate.

There seems to be an underlying assumption in iOS - if you are on a network you have internet connectivity. In our case that may or may not be true.

The bug appears to be that both the HttpWebRequest and HtptClient.GetAsync are ignoring the timeout (and also the cancellation token). It just goes off in to la-la land and returns on its own sweet time.

Anyone run in to this and has anyone found a way to mitigate it?

Steps to reproduce:

  1. Connect your iDevice to a network with no internet connectivity.
  2. Try to hit a website. Time and cancellation token are ignored.


Sign In or Register to comment.