【发布时间】:2018-02-06 16:40:13
【问题描述】:
我们正在为 Xamarin 使用 c# 和 Visual Studio 2017 开发 iOS 购物车应用程序。我们正在使用休息网络服务,在这里我无法调用网络服务。当我调用 Web 服务时,我收到带有错误 [ConnectFailure (Connection denied)] 的空响应。我的问题是如何从 localhost URL 中获取值,例如 [http://localhost:56207/api/Users/Raju/Password@123]。当我在浏览器中输入此 URL 时,我会根据用户和密码验证得到真或假。我请求您帮助我解决此问题。我将代码粘贴在下面:
public class RestInterfaceImp : IRestLogin
{
HttpClient client;
private const string WebServiceUrl = "http://localhost:56207/api/Users/Raju/Password@123";
public async Task<List<User>> RefreshDataAsync()
{
try
{
var httpClient = new HttpClient();
var resp = await httpClient.GetAsync(WebServiceUrl);
if (resp.IsSuccessStatusCode)
{
var respStr = await resp.Content.ReadAsStringAsync();
var listaAtletas = JsonConvert.DeserializeObject<List<User>>(respStr);
}
}
catch (HttpRequestException e)
{
Debug.WriteLine(e.InnerException.Message);
}
return null;
}
}
【问题讨论】:
-
您可以在浏览器中获取结果(我相信来自计算机),因为您的计算机浏览器可以访问该本地主机 URL,但是来自模拟器或真实设备的 iOS 应用程序应该如何知道并到达你的电脑?
-
据此link:iOS 模拟器使用主机网络,因此您应该能够只使用 localhost 或您的机器 IP 地址,无论您的 Web 服务正在侦听哪个 IP。
-
也就是说,在 iOS 模拟器(比如 Safari)中输入该 URL 时会得到任何结果吗?
-
我从来没有通过使用 localhost:
成功连接到 api,但其他人说这是可能的。 -
非常感谢,现在可以使用了
标签: c# xamarin.forms xamarin.ios