Taufik Hidayat Taufik Hidayat - 4 months ago 63
C# Question

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

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

Answer

[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);