I am using Xamarin.iOS 6.3.7.164 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("http://192.168.179.33/fooapi/"), Timeout = TimeSpan.FromSeconds(3) }; Console.WriteLine("Timeout is set to: " + httpClient.Timeout); Console.WriteLine("Start: " + DateTime.Now); var start = DateTime.Now; try { 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 (http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.timeout.aspx). 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:
https://github.com/mono/mono/blob/master/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs
Question: can someone reproduce this behaviour?
Posts
Because the code formatter seems to be confused by my example I am attaching a failing test project.
I just tried your project on my machine (same Xamarin.iOS version) and received this:
I run your project several times in Debug Mode with the iPhone Simulator 6.1.
With witch version? 6.3.7.164 (Beta Channel)?
I just updated the Mono Framework to version 3.1.1. The same duration like before.
Same version but from the alpha channel:
Hm ... it's reproduceable on a second machine. I will file abug.
Here we go: https://bugzilla.xamarin.com/show_bug.cgi?id=13200
Hi, I am having a similar problem.
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??
I also get a 75 second timeout, particularly when there is a valid connection, but the host is not reachable, similar to @ElenadelValleMatute.
@Jonathan_Moosekian @ElenadelValleMatute
Any progress? I'm having similar issues.
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 https://github.com/Cheesebaron/Cheesebaron.MvxPlugins/tree/master/Connectivity.
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
@Elskurkos, this was resolved in mono master. There's another bug that was submitted that indicated more specific steps on reproducing the issue here: https://bugzilla.xamarin.com/show_bug.cgi?id=24704 (which branches from the original bug https://bugzilla.xamarin.com/show_bug.cgi?id=13200).
Since this is a mono bug, I'm not sure when Xamarin will pull this in.
I had the same issue. I hope it will be fixed soon.
I'm having the same issue under Android. Is there an alternative class we can use, or a workaround?
Also would like a workaround here.
The problem is that the timeout set on HTTP client isn't taking effect if the timeout is happening below HTTP at the socket connection. Here's a workaround I'm using which so far seems to work. I use this...
instead of just calling sendAsync directly...
I set SOCKET_TIMEOUT to be a few seconds higher than the timeout I set on HttpClient just to make sure it doesn't timeout a request that could have been properly handled at the HTTP layer.
This isn't ideal - it basically leaves the HTTP request out in the ether until it times out or decides to check the cancellation token. But it does bypass the 75 sec timeout and so far I haven't seen any bad side-effects.
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...
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.
Thanks!
Is this fixed yet? The default timeout is taking up to 3 minutes when the host is unreachable...
@MarkusHerkommer Mind sharing your solution? Thanks!
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 9.6.2.4.
As a long shot, I tried switching to the beta channel and all my problems surprisingly seem to be fixed in Xamarin.iOS 9.8.0.319!
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.
Hi,
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
Thanks