gpt4 book ai didi

c# - 从远程计算机调用 web api 核心 asp.net 总是收到 400 错误请求

转载 作者:行者123 更新时间:2023-12-04 16:07:13 26 4
gpt4 key购买 nike

如果之前有人问过这个问题,我深表歉意。我们已经尝试过来自 StackOverflow 的这个链接,但对我们也不起作用 Bad Request - Invalid Hostname when accessing localhost Web API or Web App from across LAN .

我们在其中一台笔记本电脑中配置了 .net core 2.0 web api,IP 地址如下:

current .net machine IP 192.168.43.192

并在 Windows 10 和 Visual Studio 2017 上运行

Windows 10 Visual Studio Enterprise 2017 Version 15.5.2

项目设置: .net core web api project settings HTTPS Port 44350 and HTTP 51785

我们也修改了项目的 applicationsHost 配置文件,并在绑定(bind)中添加了具有正确端口的机器的 IP 地址

enter image description here

我们也运行以下命令 netsh https 添加 urlacl url= http://192.168.43.192:51785/用户=所有人 netsh advfirewall 防火墙添加规则 name="IISExpressWeb"dir=in protocol=tcp localport=51785 profile=private remoteip=localsubnet action=allow

我们在防火墙规则(入站/出站)中也包含了上述端口: Inbound Firewall Rule allowed TCP Port 44350 and 51785 Outbound firewall rule allowed TCP Port 44350 and 51785

问题是,当我们从网络上的另一台机器(不同的 ip 地址,可以是 Mac 或移动设备,通过端口 44350、ssl 或端口 51785)访问 web api 时,它总是返回错误

service unavailable - http error 501 on http 51785 bad request - invalid hostname http error 400 the request hostname is invalid on https port 44350预先感谢您的帮助。

我目前正在路上工作并使用我的手机(在上图中作为路由器,因此在 android 上没有 wifi 连接图标)。它在我的 mac 上工作相同,它也连接到 android 手机的 wifi。此外,如果我在家里使用固定路由器,我也会得到同样的结果。

我们甚至尝试从这里下载 Conveyor https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti

最佳答案

由于 IIS Express 无法接受远程连接,所以单独运行 Kestrel并在 Program.cs 文件中设置 UseUrls

WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://*:[your port]")

如果要使用 IIS Express:

你需要转发端口,似乎这个东西检查连接发送的主机头是否匹配它所绑定(bind)的或者如果没有绑定(bind)则为 127.0.0.1。

  • 停止 IIS Express
  • 打开.vs\config\applicationhost.config
  • 找到 <binding protocol="http" bindingInformation="*:[port]:localhost">
  • 将其更改为:<binding protocol="http" bindingInformation="*:[port]:" />

  • 从 cmd 创建端口转发:

netsh interface portproxy add v4tov4 listenport=[new-port] listenaddress=* connectport=[the-port-iis-using] connectaddress=127.0.0.1

  • [new-port] 添加防火墙入站规则
  • 现在您可以使用 ip 和新端口远程调用 api

关于c# - 从远程计算机调用 web api 核心 asp.net 总是收到 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48262739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com