C# Xamarin Forms Visual Studio 2015 HttpClient请求无法在iOS上运行

C# Xamarin Forms Visual Studio 2015 HttpClient请求无法在iOS上运行,c#,visual-studio-2015,xamarin.ios,xamarin.forms,C#,Visual Studio 2015,Xamarin.ios,Xamarin.forms,我对Xamarin Forms/Visual Studio相当陌生,但以下是我目前的情况。 我在VisualStudio中有一个Xamarin表单项目,其中包含Android、iOS和Windows Phone子项目。我有以下登录功能: public async Task<Tuple<bool, object>> Login(LoginCredentials credentials) { var loginUrl = apiUrl + "/

我对Xamarin Forms/Visual Studio相当陌生,但以下是我目前的情况。 我在VisualStudio中有一个Xamarin表单项目,其中包含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);
        }
    }
异常为System.NullReferenceException:Object reference未设置为对象的实例

但是,此异常仅在iOS上发生。Android和Windows Phone都可以成功发出此请求

我的第一个假设是苹果的ATS,这很有意义,因为我的服务器只是一个不使用https的开发实例。所以我贴了进去

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

我会继续调试这个,并在有任何新进展时发布另一个更新。像往常一样,非常感谢您的帮助。

我认为ATS没有任何帮助。我已经使用localhost和共享本地服务器创建了一个POC。请检查以下代码,该代码适用于我的POC

邮寄申请

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)
    {

    }
我找到了解决办法


我已将NuGet
安装包Microsoft.Net.Http
运行到便携项目中,它现在正在工作。

您确定在尝试发布请求时已实例化
客户端
吗?确定。除非出于某种原因,它会在Android和Windows上,而不会在IOS上,否则它的客户端实例仍然为空,请检查是什么导致它为空null@OlegBogdanov谢谢你,我已经用新的信息更新了这个问题
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
    }
}
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)
    {

    }