【问题标题】:Xamarin Forms Visual Studio 2015 HttpClient requests not working on iOSXamarin Forms Visual Studio 2015 HttpClient 请求在 iOS 上不起作用
【发布时间】:2017-01-12 21:12:38
【问题描述】:

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

    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()'s 中可以看出,在线抛出异常:

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 时:

调试 Android 时:

现在的任务是弄清楚为什么会在 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
    }
}

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

【问题讨论】:

  • 当您尝试 Post 您的请求时,您确定 client 被实例化了吗?
  • 是的,确实如此。除非出于某种原因,它会在 Android 和 Windows 上,而不是在 iOS 上
  • 仍然是你的客户端实例为空,检查是什么导致它为空
  • @OlegBogdanov 谢谢你我现在用新信息更新了这个问题

标签: c# visual-studio-2015 xamarin.ios xamarin.forms


【解决方案1】:

我找到了解决方案here

我将 NuGet Install-Package Microsoft.Net.Http 运行到 Portable 项目,它现在正在运行。

【讨论】:

    【解决方案2】:

    我认为 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)
        {
    
        }
    

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多