0

Can anyone tell me how to solve this problem?

This is my main page cs file: 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;

        }

no name
  • 1
  • 1
  • [Please do not upload images of code/errors when asking a question](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). – Timus Nov 19 '20 at 10:16
  • it cannot connect to the server - this is a networking issue. I'd suggest starting by using the IP or FQDN of the server, not localhost – Jason Nov 19 '20 at 13:06

1 Answers1

0

There's a sandbox in UWP which forbid to connect to localhost port not opened by your own app, it's called "Net Isolation". 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://learn.microsoft.com/en-us/previous-versions/windows/apps/dn640582(v=win.10)?redirectedfrom=MSDN

Poppyto
  • 554
  • 8
  • 17