简体   繁体   English

Http 请求异常 - 无法建立与服务器的连接 - UWP Web API

[英]Http Request Exception - A connection with the server could not be established - UWP Web API

Can anyone tell me how to solve this problem?谁能告诉我如何解决这个问题?

This is my main page cs file: MainPage.cs这是我的主页 cs 文件:MainPage.cs

        public MainPage()
        {
            InitializeComponent();
            GetAccount();
        }

        async void GetAccount()
        {
            HttpClientHandler clientHandler = new HttpClientHandler();
            clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
            // Pass the handler to httpclient(from you are calling api)
            HttpClient httpClient = new HttpClient(clientHandler);
            httpClient.Timeout = TimeSpan.FromMinutes(30);
            var api = "https://localhost:xxxxx/API/Accounts";//web api
            var response = await httpClient.GetStringAsync(api);//The error showed this line got problem
            var user = JsonConvert.DeserializeObject<List<Account>>(response);
            US.ItemsSource = user;

        }

There's a sandbox in UWP which forbid to connect to localhost port not opened by your own app, it's called "Net Isolation". UWP 中有一个沙箱,它禁止连接到您自己的应用程序未打开的本地主机端口,称为“网络隔离”。 You can grant the permission with :您可以通过以下方式授予权限:

c:\\>checknetisolation loopbackexempt -a -n=<package family name>

And drop permission :并删除权限:

c:\\>checknetisolation loopbackexempt -d -n=<package family name>

More details here : https://docs.microsoft.com/en-us/previous-versions/windows/apps/dn640582(v=win.10)?redirectedfrom=MSDN更多详细信息: https : //docs.microsoft.com/en-us/previous-versions/windows/apps/dn640582(v=win.10)?redirectedfrom=MSDN

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

相关问题 无法建立 SSL 连接,请参阅内部异常 - The SSL connection could not be established, see inner exception 无法建立 HTTP/2 连接,因为服务器未完成 HTTP/2 握手 gRPC 微服务 .net 核心 - An HTTP/2 connection could not be established because the server did not complete the HTTP/2 handshake gRPC Microservices .net core “无法建立 SSL 连接,请参阅内部异常。” 对于 POST 请求(.net 6) - "The SSL connection could not be established, see inner exception." For POST Request(.net 6) c# : 无法建立与服务器的连接 - c# : A connection with the server could not be established 如何解决System.Net.Http.WinHttpException:无法与服务器建立连接? - How to resolve System.Net.Http.WinHttpException: A connection with the server could not be established? 无法建立 SSL 连接 - The SSL connection could not be established 无法建立 SSL 连接 - SSL connection could not be established C# SSL连接无法建立,见内部异常 - C# The SSL connection could not be established, see inner exception HTTP 获取请求 SQL 服务器 web ZDB974238714CA8DE634A7CE1D083A14 - HTTP Get request SQL server web API 我可以在UWP上运行一些http服务器吗? - Could I run a some http server on UWP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM