[英]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.