簡體   English   中英

Xamarin Forms Visual Studio 2015 HttpClient 請求不適用於 iOS

[英]Xamarin Forms Visual Studio 2015 HttpClient requests not working on iOS

我對 Xamarin Forms/Visual Studio 比較陌生,但這是我目前的情況。 我在 Visual Studio 中有一個 Xamarin Forms 項目,其中包含 Android、iOS 和 Windows Phone 子項目。 我有以下功能登錄:

    public async Task<Tuple<bool, object>> Login(LoginCredentials credentials)
    {
        var loginUrl = apiUrl + "/login";
        var json = JsonConvert.SerializeObject(credentials);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        try
        {
            Debug.WriteLine("start of try block");
            HttpResponseMessage response = await client.PostAsync(loginUrl, content);
            Debug.WriteLine("this will not print on iOS");
            string bodyStr = await response.Content.ReadAsStringAsync();
            if (response.IsSuccessStatusCode)
            {
                // code that does stuff with successful response
            } else
            {
                // code that does stuff with bad response
            }
        } catch (Exception e)
        {
            Debug.WriteLine(e);
            return new Tuple<bool, object>(false, e.Message);
        }
    }

Debug.WriteLine()可以看出,該行拋出異常:

HttpResponseMessage response = await client.PostAsync(loginUrl, content);

例外是System.NullReferenceException: Object reference not set to an instance of an object

但是,此異常僅發生在 iOS 上。 Android 和 Windows Phone 都可以成功發出此請求。

我的第一個假設是它是Apple 的 ATS ,這很有意義,因為我的服務器只是一個不使用 https 的開發實例。 所以我粘貼了

<key>NSAppTransportSecurity</key>
<dict>
     <key>NSAllowsArbitraryLoads</key>
     <true/>
</dict>

到 iOS 項目的 Info.plist。 仍然沒有運氣。

我還在別處讀到了一個問題,說要確保 Windows 上的 Visual Studio 和 Mac 上的 Xamarin 都是最新的,我也確保了這一點。

另外apiUrl不是http://localhost我在我的本地網絡上使用 IP(因為 Mac 和 iOS 模擬器在不同的機器上運行),我已經確認這是正確的。

如果我不得不在這一點上猜測,我會說它仍然是 ATS 設置未正確設置的問題,但我一直無法確認這一點。 不知道從這里去哪里。 我將繼續調試和添加更新,但任何幫助將不勝感激。

更新:

正如Oleg Bogdanov所建議的,只有在運行 iOS 時,我的客戶端實例才能正確實例化。

調試iOS時: iOS HttpClient 調試

調試Android時: Android HttpClient 調試

現在的任務是弄清楚為什么在 iOS 上會發生這種情況。 這是client初始化代碼。 我已經嘗試了它當前的位置和評論的位置。

public class API
{
    public HttpClient client = new HttpClient();
    public string apiUrl = "http://myApiUrl";

    public API()
    {
        //this.client = new HttpClient();
    }

    public async Task<Tuple<bool, object>> Login(LoginCredentials credentials)
    {
        // login function code
    }
}

當我有任何新的進展時,我會繼續調試這個並發布另一個更新。 像往常一樣,非常感謝任何幫助。

我在這里找到了解決方案。

我將 NuGet Install-Package Microsoft.Net.Http到 Portable 項目,現在它正在運行。

我認為 ATS 沒有。 我已經使用localhost共享本地服務器創建了一個 POC。 請檢查以下適用於我的 POC 的代碼

對於 POST 請求

try
    {
        HttpClient client = new HttpClient();
        var uri = new Uri("http://localhost/RegisterDevice");
        string data = "{\"username\": \"ADMIN\",\"password\": \"123\"}";
        var content = new StringContent(data, Encoding.UTF8, "application/json");

        HttpResponseMessage response = null;
        response = await client.PostAsync(uri, content);
        Util.HideLoading();
        if (response.IsSuccessStatusCode)
        {
            System.Diagnostics.Debug.WriteLine(@"Success");
        }
        else
        {
            System.Diagnostics.Debug.WriteLine(@"Fail");                
        }
    }
    catch (Exception ex)
    {

    }

暫無
暫無

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

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