小编Tau*_*yat的帖子

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

我想制作应用程序,在Device Portal中将REST API实现到我的应用程序中.但是,我无法使用HttpClient连接到127.0.0.1,而另一个API就像在System.Net和Windows.Web.Http中那样熟悉,总是得到异常"无法建立与服务器的连接".

点击查看图片

但是,它只发生在RS1版本(104393)中.在TH2 build(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"); //...
            }
        }
Run Code Online (Sandbox Code Playgroud)

我也尝试使用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 = …
Run Code Online (Sandbox Code Playgroud)

c# windows uwp

5
推荐指数
1
解决办法
1328
查看次数

标签 统计

c# ×1

uwp ×1

windows ×1