Xamarin Forms Visual Studio 2015 HttpClient请求在iOS上不起作用 [英] Xamarin Forms Visual Studio 2015 HttpClient requests not working on iOS

查看:149
本文介绍了Xamarin Forms Visual Studio 2015 HttpClient请求在iOS上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Xamarin Forms/Visual Studio相当陌生,但这是我目前的情况. 我在Visual Studio中有一个带有Android,iOS和Windows Phone子项目的Xamarin Forms项目.我具有以下登录功能:

I'm rather new to Xamarin Forms/ Visual Studio but here is my current situation. I have a Xamarin Forms project in Visual Studio with Android, iOS, and Windows Phone subprojects. I have the following function Login:

    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()可以看到,在行上抛出了一个异常:

As you can see from the Debug.WriteLine()'s, an exception is thrown on the line:

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

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

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

However, this exception only occurs on iOS. Android and Windows Phone can both successfully make this request.

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

My first assumption was that it was Apple's ATS which would make sense since my server is just a dev instance not using https. So I pasted in

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

到iOS项目的Info.plist.还是没有运气.

to the iOS project's Info.plist. Still no luck.

我还在其他地方读过一个问题,该问题是要确保Windows和Mac上的Xamarin都是最新的,而我也已经确定.

I also read a question elsewhere that said to make sure Visual Studio on Windows and Xamarin on the Mac are both up to date which I have made sure of as well.

apiUrl也不是http://localhost我在本地网络上使用了IP(由于Mac和iOS模拟器在另一台计算机上运行),因此我确认是正确的.

Also apiUrl is not http://localhost I am using the IPs on my local network (since the Mac and iOS simulator are running on a different machine) which I have confirmed are correct.

如果现在我不得不猜测,我会说仍然是ATS设置没有正确设置的问题,但是我无法确认这一点.不知道从这里去哪里.我将继续调试和添加更新,但是任何帮助将不胜感激.

If I had to guess at this point, I would say it's still something with the ATS settings not being set properly but I've been unable to confirm this. Not sure where to go from here. I'll continue to debug and add updates but any help would be very much appreciated.

根据 Oleg Bogdanov 的建议,仅在运行iOS时,我的客户端实例无法正确实例化.

As suggested by Oleg Bogdanov, my client instance is not instantiated properly only when running iOS.

在调试iOS时:

在调试Android时:

When debugging Android:

现在,任务开始弄清楚为什么在iOS上发生这种情况.这是client初始化代码.我已经尝试了当前位置和评论位置.

Now the task becomes figuring out why this is occurring on iOS. Here is the client initialization code. I have tried both where it currently is and where it is commented.

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

我将继续对此进行调试,并在有任何新进展时发布另一个更新.与往常一样,任何帮助都将不胜感激.

I'll keep debugging this and post another update when I have any new developments. As usual, any help greatly appreciated.

推荐答案

我找到了解决方案我运行NuGet Install-Package Microsoft.Net.Http到Portable项目,并且现在正在运行.

I ran NuGet Install-Package Microsoft.Net.Http to the Portable project and it is working now.

这篇关于Xamarin Forms Visual Studio 2015 HttpClient请求在iOS上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆