【问题标题】:Http Request Exception - A connection with the server could not be established - UWP Web APIHttp 请求异常 - 无法建立与服务器的连接 - UWP Web API
【发布时间】:2021-03-02 15:13:12
【问题描述】:

谁能告诉我如何解决这个问题?

这是我的主页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;

        }

【问题讨论】:

标签: c# xamarin.forms webapi xamarin.uwp


【解决方案1】:

UWP 中有一个沙箱,它禁止连接到您自己的应用程序未打开的 localhost 端口,它被称为“网络隔离”。您可以通过以下方式授予权限:

c:\&gt;checknetisolation loopbackexempt -a -n=&lt;package family name&gt;

并删除权限:

c:\&gt;checknetisolation loopbackexempt -d -n=&lt;package family name&gt;

更多细节在这里:https://docs.microsoft.com/en-us/previous-versions/windows/apps/dn640582(v=win.10)?redirectedfrom=MSDN

【讨论】:

    猜你喜欢
    • 2020-04-06
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 2021-07-18
    • 2018-08-09
    • 1970-01-01
    相关资源
    最近更新 更多