从外部 IP 调用 HttpListener
Call HttpListener from external IP
我在 C# 中有一个非常简单的 API 用于测试目的。 API 是在 Windows Forms 项目中创建的
listener = new HttpListener();
listener.Prefixes.Add(url);
listener.Start();
LogUtils.appendInfo("Listening for connections on: " + url);
//Handle requests
Task listenTask = HandleIncomingConnections();
listenTask.GetAwaiter().GetResult();
如果我调用 http://localhost:8080/,我会收到来自 API 的响应。
问题是,如果我使用外部 IP 地址从另一台 PC 调用 API http://ExternalIp:8080/
我收到这个错误:
Bad Request - Invalid Hostname HTTP Error 400. The request hostname is
invalid
我尝试过的:
- 我在防火墙中为传入连接中的端口 8080 设置了规则。
- 我已经使用 ISS 服务器进行了测试,该服务器会关闭和打开 Windows Server 2019。
- 我在 'C:\Users\myUser\source\repos\myProyect.vs\proyectname\config' 中为 <'binding protocol="http" bindingInformation=":8080:" 创建了一个 'applicationhost.config ' 文件 / > 因为我的项目没有 applicationhost.config 文件。
正如@PanagiotisKanavos 评论的那样,我不能在“listener.Prefixes.Add”中使用“localhost”进行外部请求。
解决者:
http://*:8080/
并且 运行 Visual Studio 作为管理员。
我在 C# 中有一个非常简单的 API 用于测试目的。 API 是在 Windows Forms 项目中创建的
listener = new HttpListener();
listener.Prefixes.Add(url);
listener.Start();
LogUtils.appendInfo("Listening for connections on: " + url);
//Handle requests
Task listenTask = HandleIncomingConnections();
listenTask.GetAwaiter().GetResult();
如果我调用 http://localhost:8080/,我会收到来自 API 的响应。
问题是,如果我使用外部 IP 地址从另一台 PC 调用 API http://ExternalIp:8080/
我收到这个错误:
Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid
我尝试过的:
- 我在防火墙中为传入连接中的端口 8080 设置了规则。
- 我已经使用 ISS 服务器进行了测试,该服务器会关闭和打开 Windows Server 2019。
- 我在 'C:\Users\myUser\source\repos\myProyect.vs\proyectname\config' 中为 <'binding protocol="http" bindingInformation=":8080:" 创建了一个 'applicationhost.config ' 文件 / > 因为我的项目没有 applicationhost.config 文件。
正如@PanagiotisKanavos 评论的那样,我不能在“listener.Prefixes.Add”中使用“localhost”进行外部请求。
解决者:
http://*:8080/
并且 运行 Visual Studio 作为管理员。