【发布时间】:2017-04-24 05:37:24
【问题描述】:
我正在使用 Xamarin Forms 构建应用程序。我对 Android 没有任何问题,但是当我尝试模拟或部署 iOS 应用程序时,我的 HttpClient 似乎无法连接到服务器。超时到期后,我得到一个 TaskCanceledException。
HttpClient 实际上是在 iOS 应用程序引用的单独项目中使用的,如果这很重要的话。这是我的用法:
string serviceUri = service + methodName;
HttpRequestMessage request = new HttpRequestMessage(methodRequestType, serviceUri)
{
Content = new StringContent(content, Encoding.UTF8, "application/json")
};
HttpResponseMessage response = await _client.SendAsync(request).ConfigureAwait(false);
string returnString = await response.Content.ReadAsStringAsync();
return returnString;
我没有使用任何等待或访问 .Result,因为我看到很多人尝试不了解异步操作。
我看到过使用错误 HttpClient 的旧帖子(大约 2013 年)。在当前版本中这仍然是一个问题吗?我也尝试在 iOS 项目设置中更改 HttpClient 实现,但无济于事。
提前致谢!
【问题讨论】:
-
您是否连接到
https或http源?如果http你设置了ATS例外吗? -
它是一个 https 源,我刚刚用 ssllabs.com/ssltest 重新测试了端点,它报告 ATS 没有问题。
-
你能连接到任何其他网页吗?
标签: xamarin xamarin.ios xamarin.forms .net-core dotnet-httpclient