Reputation: 8425
i am using HttpClient
to fetch the data from the server in Xamarin Forms
using following code
public Task<HttpResponseMessage> sendData(String url,String jsonData)
{
using (var client = new HttpClient())
{
var jsonContent = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json");
client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
client.BaseAddress = new Uri(baseAddress);
return client.PostAsync(new Uri(url, UriKind.Relative), jsonContent);
}
}
Here i have used Timeout.Infinite
as webservice is taking around 4 mins to return the response but although i have set the Timeout.Infinite
app is throwing Operation time out
exception. I tried with the various timeouts as well like 240000ms
but still getting operation timeout.
However, when i try to fire the same request in postman it is working but yes returning the response after 4 mins of time. As it is a third party api, we can not change anything there.
can anyone please suggest how can i forced the HttpClient
to hold more for the response.
Detail exception i am getting
{System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: The operation has timed out.
at System.Net.HttpWebRequest+<RunWithTimeoutWorker>d__241`1[T].MoveNext () [0x000c5] in <3e9b3e26c4694baab3f689687ad40612>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00020] in <3e9b3e26c4694baab3f689687ad40612>:0
at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <d4a23bbd2f544c30a48c44dd622ce09f>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Net.Http.HttpClientHandler+<SendAsync>d__64.MoveNext () [0x0041d] in <25ebe1083eaf4329b5adfdd5bbb7aa57>:0
--- End of inner exception stack trace ---
at System.Net.Http.HttpClientHandler+<SendAsync>d__64.MoveNext () [0x00478] in <25ebe1083eaf4329b5adfdd5bbb7aa57>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Net.Http.HttpClient+<SendAsyncWorker>d__49.MoveNext () [0x000ca] in <25ebe1083eaf4329b5adfdd5bbb7aa57>:0
--- End of stack trace from previous location where exception was thrown ---
at SignodeMSA.WebService.HttpService+<sendData>d__13.MoveNext () [0x0009c] in D:\VsualStudioWorkspace\SignodeMSA\SignodeMSA\SignodeMSA\WebService\HttpService.cs:141
--- End of stack trace from previous location where exception was thrown ---
at SignodeMSA.WebService.RestApi+<sendDataSimple>d__96.MoveNext () [0x000b3] in D:\VsualStudioWorkspace\SignodeMSA\SignodeMSA\SignodeMSA\WebService\RestApi.cs:199 }
Upvotes: 1
Views: 3614
Reputation: 8425
I have tried following code for Android and iOS and it seems working and can wait little longer on socket while fetching data using webservice
in Xamarin.Android
following code is used and exposed it using DependencyService
public HttpClientHandler GetHttpClientHandler()
{
Xamarin.Android.Net.AndroidClientHandler http = new Xamarin.Android.Net.AndroidClientHandler();
http.ReadTimeout = TimeSpan.FromMinutes(5.0);
return http;
}
in Xamarin.iOS
following code is used and exposed it using DependencyService
HttpMessageHandler IHttpHandler.GetHttpHandler()
{
NSUrlSessionConfiguration sessionConfig = NSUrlSessionConfiguration.DefaultSessionConfiguration;
sessionConfig.TimeoutIntervalForRequest = 300;
sessionConfig.TimeoutIntervalForResource = 300;
sessionConfig.WaitsForConnectivity = true;
NSUrlSessionHandler sessionHandler = new NSUrlSessionHandler(sessionConfig);
return (sessionHandler);
}
Note : in case of iOS it will return HttpMessageHandler
Usage in Xamarin.Forms
HttpClient client = new HttpClient(DependencyService.Get<IHttpClientHandler>().GetHttpClient());
However, there is one catch over here , in case of iOS if app is fetching the data using HttpClient and if user locks the screen by pressing the lock button in iPhone then the service gets immediately interrupted and throws Connection Interrupted
and socket gets closed. if anyone could find any solution to this then feel free to post
Upvotes: 2
Reputation: 321
Try using the InfiniteTimeSpan from the System.Threading.Timeout namespace.
client.Timeout = System.Threading.Timeout.InfiniteTimeSpan;
I'm using it on my Xamarin projects, and it appears to be to working fine.
Upvotes: 0