簡體   English   中英

在 Xamarin.Forms iOS 中,獲取 HttpRequestException:鎖定設備時發送請求時出錯

[英]In Xamarin.Forms iOS, Getting HttpRequestException: An error occurred while sending the request when locking the device

在 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 中,它運行良好。

使用嘗試捕獲:

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;
    }
}

首先是您以錯誤的方式使用 HttpClient。 這將導致多個請求,並可能產生可能使您的應用程序崩潰的錯誤。 接下來,您應該檢查您正在使用的 http 客戶端實現的 iOS 項目選項。 如果您進行 https 調用,請確保 http 客戶端實現正確處理 tls 版本。 請記住,默認托管實現只能處理 tls 1.0。

參考:

MS 文檔 HttpClient

文章 ASP.NET 怪物

MS 文檔 iOS HttpClient

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM