【问题标题】:UWP App cannot resolve local domain from hosts fileUWP 应用无法从主机文件解析本地域
【发布时间】:2020-02-08 14:50:59
【问题描述】:

我有一个修改过的hosts文件如下:

192.168.10.10 library.test

我尝试使用 HttpClient 从 UWP 应用程序连接到该域

var client = new HttpClient();

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

但这会失败并出现以下异常

  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

同样的代码在使用https://google.com这样的主机时也能正常工作

【问题讨论】:

标签: c# windows uwp


【解决方案1】:

我没有重现你的问题,当我尝试修改主机中的网站library.test并指向本地网页时,可以正常访问。

请检查以下内容:

  1. 192.168.10.10 是可访问的 IP 地址吗?如果只访问本地,可以改成127.0.0.1

  2. 如果不设置端口号,则默认访问80端口,请确保该端口下有可访问的内容。

【讨论】:

  • 经过 Postman 和主机文件修改测试并按预期工作,但由于某种原因,UWP 应用程序无法工作。我也启用了专用网络功能以防万一
  • UWP 对本地网络环回有限制。如果您的 IP 地址指向本地主机,它将被拒绝访问。但是,在调试状态下,Visual Studio 默认会解除网络环回限制。您可以检查您的project settings 以查看本地网络环回是否已打开。如果想取消release状态下的网络环回,可以查看这个Document
  • 网络环回已启用。我想知道这是否与主机修改指向虚拟网络适配器中的地址这一事实有关。作为参考,我正在尝试连接到在我的机器上本地运行的虚拟机。它在 UWP 之外可以正常工作(例如使用 Postman),但在 UWP 内部却不行
  • 这是一个奇怪的现象。首先我们需要确认你提供的IP地址是本地地址还是网络地址?如果我去掉hosts文件中的重定向,可以直接访问IP地址吗?
  • 是的,我仍然可以直接访问 IP。我不确定内部结构,但这个特定的 IP 似乎是一种访问虚拟机本地主机的方法?
猜你喜欢
  • 2012-10-27
  • 2018-11-27
  • 2016-01-09
  • 2013-11-28
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
相关资源
最近更新 更多