Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to 127.0.0.1 (localhost) Device Portal in UWP by HttpClient or else (Windows 10 Mobile RS1 only)

Tags:

c#

windows

uwp

I want to make apps to implement REST API's in Device Portal into my apps. But, I can't connect to 127.0.0.1 with HttpClient and another API's smiliar like that even in System.Net and Windows.Web.Http , always got exception "connection with the server could not be established" .

Click to see image

But, It's ONLY happen in RS1 build (104393) . In TH2 build (10568) anything work like charm.

Here's my code:

when i use 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"); //...
            }
        }

I'm also try use System.Net , same got exception

private async void dvInfo_Click(object sender, RoutedEventArgs e)
        {
            try
            {
        Uri uri = new Uri("http://127.0.0.1/api/os/machinename");
        var httpClient = new HttpClient();
        var ResponseString = await httpClient.GetAsync(uri); //got exception here, ResponseString null
                txtStatus.Text = ResponseString.ToString();
            }
            catch (Exception ex)
            {
                var msg = new MessageDialog(ex, "Sorry, we got some error"); //...
            }
        }

Help :3

like image 337
Taufik Hidayat Avatar asked Oct 18 '22 04:10

Taufik Hidayat


2 Answers

I'm assuming that this is on Mobile. If it's on desktop or another platform, ensure you're using the correct port first.

Have you tried targeting the phone from another device? e.g. run the same app using a your phone's IP address on your PC. It may work that way. The reason for this is loopback protection, where apps are prevented from connecting to APIs on the same device as a security precaution. You may be able to enable local loopback in your app's build options in VS, at the link posted.

like image 66
Hirsch Singhal Avatar answered Oct 20 '22 16:10

Hirsch Singhal


[Solved] I must move to Windows.Web.Http then use https uri inside http , afterthat do pass cert/ignore that:

var filter = new HttpBaseProtocolFilter();
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);
var http = new HttpClient(filter);
like image 25
Taufik Hidayat Avatar answered Oct 20 '22 17:10

Taufik Hidayat