Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling web api core asp.net from remote computer always gets 400 bad request

Apologies if this could have been asked before. We hve tried this link from StackOverflow and didn't work for us either Bad Request - Invalid Hostname when accessing localhost Web API or Web App from across LAN.

We have a .net core 2.0 web api configured in one of the laptops with the following ip address:

current .net machine IP 192.168.43.192

and running on windows 10, and Visual Studio 2017

Windows 10 Visual Studio Enterprise 2017 Version 15.5.2

The project settings: .net core web api project settings HTTPS Port 44350 and HTTP 51785

We have modified the applicationsHost config file as well for the project and added the IPAddress of the machine with the correct port on the bindings

enter image description here

We run the following commands too netsh https add urlacl url=http://192.168.43.192:51785/ user=everyone netsh advfirewall firewall add rule name="IISExpressWeb" dir=in protocol=tcp localport=51785 profile=private remoteip=localsubnet action=allow

We have included the said ports as well on the firewall rules (inboud/outbound): Inbound Firewall Rule allowed TCP Port 44350 and 51785 Outbound firewall rule allowed TCP Port 44350 and 51785

The problem is that when we access the web api from a different machine on the network (different ip address, can be a Mac or a mobile device either via port 44350, ssl, or port 51785) it always return an error

service unavailable - http error 501 on http 51785 bad request - invalid hostname http error 400 the request hostname is invalid on https port 44350 Thanks in advance for the help.

I'm currently working on the road and using my phone (on the picture above as the router hence no wifi connection icon on the android). It works the same on my mac which is connected to the android phone's wifi as well. Also, I get the same if I'm using a fixed router at home.

We even tried to download Conveyor from here https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti

like image 504
Travellogger Avatar asked Mar 09 '26 20:03

Travellogger


2 Answers

As IIS Express is stupid to accept remote connections, run Kestrel alone and set UseUrls in Program.cs file

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

If want to use IIS Express:

you need to forward port, it seems that this thing checks if the connection sends host header that matches what it is bound with or being 127.0.0.1 if not bound.

  • Stop IIS Express
  • Open .vs\config\applicationhost.config
  • Find <binding protocol="http" bindingInformation="*:[port]:localhost">
  • Change it to: <binding protocol="http" bindingInformation="*:[port]:" />

  • Create port forwarding, from cmd:

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

  • Add firewall inbound rule for [new-port]
  • now you can call the api remotely using the ip and new port
like image 159
Mohamad Alallan Avatar answered Mar 12 '26 08:03

Mohamad Alallan


Did you try our extension 'Conveyor' that was mentioned in that post? You can get Conveyor from the extensions dialog or https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti it fixes the 400 Bad Request error from IIS Express without any config changes.

like image 38
Jim W says reinstate Monica Avatar answered Mar 12 '26 10:03

Jim W says reinstate Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!