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
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.
[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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With