0

I have a modified hosts file which is as follows:

192.168.10.10 library.test

and I try to connect to this domain from a UWP app using HttpClient like so

var client = new HttpClient();

var result = await client.GetAsync("http://library.test");
Console.WriteLine(result.StatusCode);

but this fails with the following exception

  HResult=0x80072EFD
  Message=An error occurred while sending the request.
  Source=System.Net.Http
  StackTrace:
   at System.Net.Http.HttpClientHandler.<SendAsync>d__111.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at WordsmithsApp.ViewModels.LoginViewModel.<TryLogin>d__4.MoveNext() in C:\Users\dubif\mods-n-stuff\Coding\Projects\WordsmithsApp\WordsmithsApp\WordsmithsApp\ViewModels\LoginViewModel.cs:line 37




Inner Exception 1:
COMException: The text associated with this error code could not be found.

A connection with the server could not be established

This same code works fine when using a host such as https://google.com

Satori
  • 1
  • 1
  • Is 192.168.10.10 a different machine, or is it the same machine you are running your app on? If it's the same machine, you might want to check out this article: https://stackoverflow.com/questions/34589522/cant-see-localhost-from-uwp-app – Greg Thatcher Feb 12 '20 at 03:07

1 Answers1

0

I did not reproduce your problem, when I try to modify the website library.test in the host and direct it to the local webpage, it can be accessed normally.

Please check the following:

  1. Is 192.168.10.10 an accessible IP address? If you only access the local, you can change to 127.0.0.1.

  2. If you do not set a port number, then the default is access to port 80, please ensure that there is accessible content under this port.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Richard Zhang
  • 7,523
  • 1
  • 7
  • 13
  • Tested with Postman and the hosts file modification and works as intended but for some reason the UWP app won't work. I have enabled the private networks capability too just in case – Satori Feb 11 '20 at 11:46
  • UWP has restrictions on local network loopback. If your IP address is pointing to a local host, it will be denied access. However, in the debug state, Visual Studio will lift the network loopback restriction by default. You can check your [project settings](https://imgur.com/VEWbu2O) to see if the local network loopback is turned on. If you want to cancel the network loopback in the released state, you can view this [Document](https://learn.microsoft.com/en-us/previous-versions/windows/apps/dn640582(v=win.10)?redirectedfrom=MSDN) – Richard Zhang Feb 12 '20 at 00:36
  • Network loopback is enebled. I wonder if it's to do with the fact that the hosts modification is pointing to an address in a virtual network adapter. For reference I'm trying to connect to a virtual machine running locally on my machine. It works fine outside of UWP (such as using Postman) but not inside UWP – Satori Feb 12 '20 at 13:03
  • This is a strange phenomenon. First of all, we need to confirm that the IP address you provided is a local address or a network address? If I remove the redirection in the hosts file, can you directly access the IP address? – Richard Zhang Feb 13 '20 at 00:20
  • Yes I can still access the IP directly. I'm not sure on the internals but this specific IP seems to be a way to access the localhost of the virtual machine potentially? – Satori Feb 13 '20 at 12:08
  • Since this problem cannot be reproduced (I can redirect to `library.test` on my device), I suggest you can consider directly accessing the IP address in UWP – Richard Zhang Feb 14 '20 at 00:10
  • I have noticed that `Dns.GetHostAddresses("library.test")[0]` does actually resolve the correct IP which is odd. Maybe this is a bug with HttpClient itself? – Satori Feb 16 '20 at 10:19
  • I have now realised that HttpClient also fails with the IP directly too – Satori Feb 16 '20 at 10:30
  • I'm not sure if this problem is caused by `HttpClient`, you can try to create a connection to `library.test` using `HttpWebRequest`. If this does not work, then this may be a gateway or proxy issue. – Richard Zhang Feb 17 '20 at 00:25
  • Same error as HttpClient. `System.Net.WebException An error occurred while sending the request. ` – Satori Feb 17 '20 at 13:01
  • Then this should not be a problem with HttpClient, nor is it a redirection set by UWP cannot access the hosts file. This IP may be an address that UWP cannot access in the sandbox – Richard Zhang Feb 18 '20 at 02:26
  • Taht makes sense. Do you know of any methods around this? I only need to access this IP for development and the release product will connect to an actual remote server. – Satori Feb 18 '20 at 07:14
  • I'm not sure why this IP is not accessible, but you can try some other IPs. If you are accessing a local address (such as `localhost`), UWP is accessible in debug state. – Richard Zhang Feb 18 '20 at 08:14