【问题标题】:In Xamarin.Forms iOS, Getting HttpRequestException: An error occurred while sending the request when locking the device在 Xamarin.Forms iOS 中,获取 HttpRequestException:锁定设备时发送请求时出错
【发布时间】:2020-01-24 20:18:20
【问题描述】:

在 Xamarin Forms iOS 中,我有页面并通过从 API 获取数据来填充一些信息。在启动页面时,我对 Appearing 事件进行 API 调用。当时,在完成页面加载之前,锁定设备。那时,我面临 HttpRequestException:发送请求时发生错误。异常详情如下,

System.Net.WebConnection.CreateStream (System.Net.WebOperation operation, System.Boolean reused, System.Threading.CancellationToken cancellationToken) [0x00208] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:234
at System.Net.WebConnection.InitConnection (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x000f7] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:263 
at System.Net.WebOperation.Run () [0x00052] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System/System.Net/WebOperation.cs:268 
at System.Net.WebCompletionSource`1[T].WaitForCompletion () [0x00094] in <3a248b3a8d824cb189d202decc560ac6>:0 
at System.Net.HttpWebRequest.RunWithTimeoutWorker[T] (System.Threading.Tasks.Task`1[TResult] workerTask, System.Int32 timeout, System.Action abort, System.Func`1[TResult] aborted, System.Threading.CancellationTokenSource cts) [0x000f8] in <3a248b3a8d824cb189d202decc560ac6>:0 
at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00019] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System/System.Net/HttpWebRequest.cs:1200 
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 <a6e593f3cb7d44ddb2035eb114e94ff7>:0 
--- End of stack trace from previous location where exception was thrown --- 
at System.Net.Http.MonoWebRequestHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x003d1] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System.Net.Http/MonoWebRequestHandler.cs:495 

请在下方找到 API 请求代码,

private async Task<MyObject> Sample(string requestUri)
{
    var client = new HttpClient
    {
        Timeout = TimeSpan.FromSeconds(60)
    };

    var response = await client.GetStringAsync(requestUri);

    var settings = new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Ignore,
        MissingMemberHandling = MissingMemberHandling.Ignore
    };

    return JsonConvert.DeserializeObject<MyObject>(response, settings);
}

注意:在 Android 中,它运行良好。

【问题讨论】:

  • 基本上,iOS 是在手机关闭时取消 HTTP 请求。为了处理这个问题,可以创建一个 onSleep 方法,手动取消请求或设置在手机下次开机时发送的请求:docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…
  • 如果 API 被取消,那么这不是问题。但是得到HttpRequestExpception,所以我无法处理它。有没有其他办法处理?
  • 使用 try catch 并专门捕获 HttpRequestExceptions
  • @GanesanVG 你好,如果有答案,记得有空时标记一下:)

标签: c# ios xamarin xamarin.forms xamarin.ios


【解决方案1】:

使用try catch:

private async Task<MyObject> Sample(string requestUri)
{
    try{
        var client = new HttpClient
        {
            Timeout = TimeSpan.FromSeconds(60)
        };

        var response = await client.GetStringAsync(requestUri);

        var settings = new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore,
            MissingMemberHandling = MissingMemberHandling.Ignore
        };

        return JsonConvert.DeserializeObject<MyObject>(response, settings);
    }
    catch(HttpRequestExceptions ex){

        return default;
    }
}

【讨论】:

    【解决方案2】:

    首先你以错误的方式使用 HttpClient。这将导致多个请求,并且可能会产生可能导致您的应用程序崩溃的错误。接下来,您应该检查您正在使用的 http 客户端实现的 iOS 项目选项。如果您进行 https 调用,请确保 tls 版本由 http 客户端实现正确处理。请记住,默认托管实现只能处理 tls 1.0。

    参考资料:

    MS Documentation HttpClient

    Article ASP.NET Monsters

    MS Documentation iOS HttpClient

    【讨论】:

    • 你能建议我最好的方法吗?有参考吗?
    猜你喜欢
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    相关资源
    最近更新 更多