Forum Xamarin.iOS

System.Net.Http.HttpClient Timeout seems to be ignored

ThomasBandtThomasBandt DEDeveloper Group Leader ✭✭
edited July 2013 in Xamarin.iOS

I am using Xamarin.iOS and facing a very strange behaviour with timing out requests sent with HttpClient():

The following code tries to get a result from an url that does not exist respectively isn't available (simulating a server outage):

private async void Connect()
    var httpClient = new System.Net.Http.HttpClient
        BaseAddress = new Uri(""),
        Timeout = TimeSpan.FromSeconds(3)

    Console.WriteLine("Timeout is set to: " + httpClient.Timeout);
    Console.WriteLine("Start: " + DateTime.Now);

    var start = DateTime.Now;

        await httpClient.GetAsync("foo");
    catch (Exception e)
        Console.WriteLine("Error: " + e.Message);

    Console.WriteLine("Finished: " + DateTime.Now);
    Console.WriteLine("Duration: " + (DateTime.Now - start).TotalSeconds);

I expected to see a result after 3 seconds, or at least after 15 seconds ( But what I get is a task running for 75 seconds:

2013-07-12 00:06:21.134 XamariniOSHttpClientConnectionTimeout[50422:c07] Timeout is set to: 00:00:03
2013-07-12 00:06:21.146 XamariniOSHttpClientConnectionTimeout[50422:c07] Start: 12.07.2013 00:06:21
2013-07-12 00:07:36.613 XamariniOSHttpClientConnectionTimeout[50422:c07] Error: Error: ConnectFailure (Connection timed out)
2013-07-12 00:07:36.614 XamariniOSHttpClientConnectionTimeout[50422:c07] Finished: 12.07.2013 00:07:36
2013-07-12 00:07:36.617 XamariniOSHttpClientConnectionTimeout[50422:c07] Duration: 75,468696

I can't see a bug here, the timeout seems to be set correctly:

Question: can someone reproduce this behaviour?


  • ThomasBandtThomasBandt DEDeveloper Group Leader ✭✭

    Because the code formatter seems to be confused by my example I am attaching a failing test project.

  • TimurZanagarTimurZanagar DE ✭✭
    edited July 2013

    I just tried your project on my machine (same Xamarin.iOS version) and received this:

    2013-07-12 00:40:12.397 XamariniOSHttpClientConnectionTimeout[2530:c07] Timeout is set to: 00:00:03
    2013-07-12 00:40:12.410 XamariniOSHttpClientConnectionTimeout[2530:c07] Start: 7/12/2013 12:40:12 AM
    Thread started:  #3
    Thread started:  #4
    Thread started:  #5
    Thread started:  #6
    2013-07-12 00:40:18.979 XamariniOSHttpClientConnectionTimeout[2530:c07] Error: Error: ConnectFailure (Network is unreachable)
    2013-07-12 00:40:18.980 XamariniOSHttpClientConnectionTimeout[2530:c07] Finished: 7/12/2013 12:40:18 AM
    2013-07-12 00:40:18.982 XamariniOSHttpClientConnectionTimeout[2530:c07] Duration: 6.569446

    I run your project several times in Debug Mode with the iPhone Simulator 6.1.

  • ThomasBandtThomasBandt DEDeveloper Group Leader ✭✭

    With witch version? (Beta Channel)?

  • TimurZanagarTimurZanagar DE ✭✭
    edited July 2013

    I just updated the Mono Framework to version 3.1.1. The same duration like before.

    Same version but from the alpha channel:

    Hash: b7e6b95
    Build date: 2013-03-07 10:27:05-0400
  • ThomasBandtThomasBandt DEDeveloper Group Leader ✭✭

    Hm ... it's reproduceable on a second machine. I will file abug.

  • ThomasBandtThomasBandt DEDeveloper Group Leader ✭✭
  • ElenadelValleMatuteElenadelValleMatute GBUniversity ✭✭

    Hi, I am having a similar problem.

                client.Timeout ==new System.TimeSpan(0,0,0,1,0);
                    getDataResponse = client.PostAsync(connectionstring, content).Result;
                }catch(Exception e){
                    toReturn.error = Results.ConnectionError;
                    return toReturn;

    It works ok when there is not connection, but when I enable the data connection, but it hasn´t got a real connection, the HttpClient keep waiting.

    What am I doing wrong??

  • Jonathan_MoosekianJonathan_Moosekian USUniversity ✭✭

    I also get a 75 second timeout, particularly when there is a valid connection, but the host is not reachable, similar to @ElenadelValleMatute.

  • ElskurkosElskurkos USMember

    @Jonathan_Moosekian‌ @ElenadelValleMatute‌

    Any progress? I'm having similar issues.

  • ElenadelValleMatuteElenadelValleMatute GBUniversity ✭✭

    I didn't check if the issue is solved. I am still using timeout, but I ping first to see if there is connection to the host with the plugin

  • nick5454nick5454 USUniversity ✭✭✭

    I would test via Reachability first. I use a zero address socket to determine. From there via Rest client I get a good time out. I do know that Safari has issues in pre ios8 and we had to set the timeout on the server side as Safari has mounds of timeouts being ignored.

    Although I have been able to use timeouts successfully on ios8

  • Jonathan_MoosekianJonathan_Moosekian USUniversity ✭✭

    @Elskurkos‌, this was resolved in mono master. There's another bug that was submitted that indicated more specific steps on reproducing the issue here: (which branches from the original bug

    Since this is a mono bug, I'm not sure when Xamarin will pull this in.

  • CihanCilCihanCil TRMember ✭✭

    I had the same issue. I hope it will be fixed soon.

  • ConradBrayConradBray GBMember

    I'm having the same issue under Android. Is there an alternative class we can use, or a workaround?

  • RandomBaresRandomBares USMember

    Also would like a workaround here.

  • alonmatalonalonmatalon ILMember

    I have the same issue.
    All my http calls via HttpClient to my API's works fine except one API whick is a long processing API (about 5-8 seconds).
    I see the API is called and return correct vakues (using server logs) but nothing is returns.

    I also created a test API that get the number of seconds to sleep before returning value.
    The api returns data on small processing time and nothing on processing time above ~5 seconds.

    setting large value on Timeout propery of HttpClient didn't help.

  • I had the same problem too...
    I have found a workaround for it by calling a timer before calling get or post method from httpclient.
    In the callback i call CancelPendingRequests and Dispose from the httpclient. Now the call fall in an exception and I set a http timeout error by myself... Works fine for me...

  • BryantYoungBryantYoung USMember

    Hi MarkusHerkommer,

    Do you have sample code for this? I have a long query that does not return and would like to use your workaround unless someone else is able to provide one for HttpClient.


  • LusFernandesLusFernandes PTMember ✭✭

    Is this fixed yet? The default timeout is taking up to 3 minutes when the host is unreachable... :neutral:

  • LusFernandesLusFernandes PTMember ✭✭

    @MarkusHerkommer Mind sharing your solution? Thanks!

  • crates_barrelscrates_barrels USMember

    I struggled 2 days searching for a workaround, but none seemed to fully solve the issue.
    I was using the stable release of Xamarin.iOS
    As a long shot, I tried switching to the beta channel and all my problems surprisingly seem to be fixed in Xamarin.iOS!

  • QuakeulfQuakeulf NOMember ✭✭✭

    Is there a hardcoded default timeout for the requests? I am doing POST- and GET-requests towards an external device that might take a bit of time to response but it seems the requests time out on their own after a few minutes time.

  • MohammedismailsameerMohammedismailsameer USMember ✭✭
    edited December 2017

    i am doing xamarin project using icelink and i have faced one issue in connect the server.for http request timeout error when connect to server please let me know how to solve that? and please give me any suggestion

Sign In or Register to comment.