可以';t通过HttpClient或其他方式(仅限Windows 10 Mobile RS1)连接到UWP中的12
本文关键字:RS1 Mobile Windows 连接 中的 UWP 仅限 通过 HttpClient 方式 其他 | 更新日期: 2023-09-27 17:59:47
我想让应用程序在我的应用程序中实现设备门户中的REST API。但是,即使在System.Net和Windows.Web.Http中,我也无法用HttpClient和另一个API的smiliar连接到127.0.0.1,总是出现"无法建立与服务器的连接"的异常。
点击查看图像
但是,它只发生在RS1版本(104393)中。在TH2版本(10568)中,任何东西都像魅力一样发挥作用。
这是我的代码:
当我使用Windows.Web.Http
时
private async void dvInfo_Click(object sender, RoutedEventArgs e)
{
try
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.TryParseAdd("*/*");
string ResponseString = await httpClient.GetStringAsync(new Uri("http://127.0.0.1/api/os/machinename")); //got exception here, ResponseString null
txtStatus.Text = ResponseString.ToString();
}
catch (Exception ex)
{
var msg = new MessageDialog(ex, "Sorry, we got some error"); //...
}
}
我也尝试使用System.Net
,同样得到了异常
private async void dvInfo_Click(object sender, RoutedEventArgs e)
{
try
{
Uri uri = new Uri("http://127.0.0.1/api/os/machinename");
var httpClient = new HttpClient();
var ResponseString = await httpClient.GetAsync(uri); //got exception here, ResponseString null
txtStatus.Text = ResponseString.ToString();
}
catch (Exception ex)
{
var msg = new MessageDialog(ex, "Sorry, we got some error"); //...
}
}
帮助:3
我假设这是在Mobile上。如果它在桌面或其他平台上,请确保首先使用正确的端口。
你是否尝试过从其他设备瞄准手机?例如,在电脑上使用手机的IP地址运行相同的应用程序。它可能会这样工作。原因是环回保护,作为安全预防措施,阻止应用程序连接到同一设备上的API。您可以在发布的链接中,在VS中的应用程序构建选项中启用本地环回。
[已解决]我必须移动到Windows.Web.Http
,然后在http
中使用https
uri,然后通过证书/忽略:
var filter = new HttpBaseProtocolFilter();
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);
var http = new HttpClient(filter);