简体   繁体   English

无法通过HttpClient连接到UWP中的127.0.0.1(localhost)设备门户(仅限Windows 10 Mobile RS1)

[英]Can't connect to 127.0.0.1 (localhost) Device Portal in UWP by HttpClient or else (Windows 10 Mobile RS1 only)

I want to make apps to implement REST API's in Device Portal into my apps. 我想制作应用程序,在Device Portal中将REST API实现到我的应用程序中。 But, I can't connect to 127.0.0.1 with HttpClient and another API's smiliar like that even in System.Net and Windows.Web.Http , always got exception "connection with the server could not be established" . 但是,我无法使用HttpClient连接到127.0.0.1,而另一个API就像在System.Net和Windows.Web.Http中那样熟悉,总是得到异常“无法建立与服务器的连接”。

Click to see image 点击查看图片

But, It's ONLY happen in RS1 build (104393) . 但是,它只发生在RS1版本(104393)中。 In TH2 build (10568) anything work like charm. 在TH2 build(10568)中,任何东西都像魅力一样。

Here's my code: 这是我的代码:

when i use Windows.Web.Http 当我使用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"); //...
            }
        }

I'm also try use System.Net , same got exception 我也尝试使用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"); //...
            }
        }

Help :3 帮助:3

I'm assuming that this is on Mobile. 我假设这是在移动设备上。 If it's on desktop or another platform, ensure you're using the correct port first. 如果它位于桌面或其他平台上,请确保首先使用正确的端口。

Have you tried targeting the phone from another device? 您是否尝试过从其他设备定位手机? eg run the same app using a your phone's IP address on your PC. 例如,使用手机上的IP地址运行相同的应用程序。 It may work that way. 它可能会那样工作。 The reason for this is loopback protection , where apps are prevented from connecting to APIs on the same device as a security precaution. 这样做的原因是环回保护 ,其中防止应用程序连接到同一设备上的API作为安全预防措施。 You may be able to enable local loopback in your app's build options in VS, at the link posted. 您可以在发布的链接中在VS的应用程序构建选项中启用本地环回。

[Solved] I must move to Windows.Web.Http then use https uri inside http , afterthat do pass cert/ignore that: [已解决]我必须转移到Windows.Web.Http然后在http使用https uri,然后通过cert / ignore:

var filter = new HttpBaseProtocolFilter();
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);
var http = new HttpClient(filter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM