Xamarin iOS HttpClient超时不起作用

编程入门 行业动态 更新时间:2024-10-28 12:16:46
本文介绍了Xamarin iOS HttpClient超时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

尽管我们将HTTP框架的默认值设置为3分钟,但在某个时刻,我们发现应用程序中的所有请求在iOS上的超时都为60秒.我尝试了以下代码来确定是否是库出现了问题:

At some point we found all the requests in our app have timeout of 60 seconds on iOS though we set the default value for the HTTP framework we use as 3 minutes. I tried the following piece of code to figure out if it's the library who has an issue:

try { using (var http = new HttpClient()) { http.Timeout = TimeSpan.FromMinutes(1.5); await http.GetAsync("httpstat.us/200?sleep=70000"); } } catch (Exception ex) { }

尽管超时设置为90秒,并且请求持续70秒,但此代码失败并带有超时异常.事实证明,它不会覆盖默认的60秒超时.相同的代码在新项目上效果很好.

This code fails with the timeout exception though the timeout is set as 90 sec and the request goes for 70 sec. Turns out it doesn't override the default timeout of 60 sec. The same code works well on the fresh project.

在项目文件中,我们有<MtouchHttpClientHandler>NSUrlSessionHandler</MtouchHttpClientHandler>

In the project file we have <MtouchHttpClientHandler>NSUrlSessionHandler</MtouchHttpClientHandler>

推荐答案

如果您不创建自己的NSUrlSessionHandler实例并提供一个自定义NSUrlSessionConfiguration在其.ctor中.

Xamarin's NSUrlSessionHandler uses the default NSUrlSessionConfiguration if you are not creating your own instance of the NSUrlSessionHandler and providing a custom NSUrlSessionConfiguration in its .ctor.

在iOS中默认的NSUrlSessionConfiguration超时设置为60秒.

The default NSUrlSessionConfiguration timeouts are set to 60 seconds in iOS.

因此,在您的Xamarin.iOS应用程序项目中,打开AppDelegate.cs并在FinishedLaunching替代项内设置默认的会话超时参数.

So in your Xamarin.iOS application project, open the AppDelegate.cs and set the default session timeout parameters within the FinishedLaunching override.

NSUrlSessionConfiguration.DefaultSessionConfiguration.TimeoutIntervalForRequest = 90.0; NSUrlSessionConfiguration.DefaultSessionConfiguration.TimeoutIntervalForResource = 90.0;

timeoutIntervalForRequest

timeoutIntervalForRequest The timeout interval to use when waiting for additional data.

timeoutIntervalForResource

timeoutIntervalForResource

The maximum amount of time that a resource request should be allowed to take.

re: developer .apple/documentation/foundation/nsurlsessionconfiguration#//apple_ref/occ/instp/NSURLSessionConfiguration/timeoutIntervalForRequest

更多推荐

Xamarin iOS HttpClient超时不起作用

本文发布于:2023-05-31 12:02:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/391395.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   Xamarin   iOS   HttpClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!