88

When I try to hit my web app on port 8080 I get the following error

Bad Request - Invalid Hostname
HTTP Error 400. The request hostname is invalid.

I don't even know where to begin to diagnose this problem

MrWhite
  • 23,175
  • 4
  • 44
  • 71
burnt1ce
  • 12,985
  • 28
  • 96
  • 146

15 Answers15

76

Did you check the binding is IIS? (inetmgr.exe) It may not be registered to accept all hostnames on 8080.

For example, if you set it up for mysite.com:8080 and hit it at localhost:8080, IIS will get the request but not have a hostname binding to match so it rejects.

Outside of that, you should check the IIS logs (C:\inetpub\logs\wmsvc#) on the server and see if you are seeing your request. Then you'll know if its a problem on your client or on the server itself.

Taylor Bird
  • 7,347
  • 22
  • 31
32

FWIW, if you'd like to just allow requests directed to any hostname/ip then you can set your binding like so:

<binding protocol="http" bindingInformation="*:80:*" />

I use this binding so that I can load a VM with IE6 and then debug my application.


EDIT: While using IIS Express to debug, the default location for this option's config file is

C:\Users\{User}\Documents\IISExpress\config\applicationhost.config
StaRbUck42
  • 117
  • 1
  • 8
Jeff LaFay
  • 11,884
  • 12
  • 70
  • 97
  • 2
    You can also get IIS Express to work with remote requets using our free VS Extension called Conveyor (involves no config changes). Use Tools->Extensions... to get it, or at https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti – Jim W says reinstate Monica Mar 17 '18 at 00:49
  • IIS was giving me "access denied" errors for the "*" host. I had to run Visual Studio with Administrator Privileges to fix it. – sigod Jan 22 '21 at 14:06
28

This page by Microsoft describes how to set up access to IIS Server Express from other computers on the local network.

In a nutshell:

1) from a command prompt with admin privileges:

netsh http add urlacl url=http://[your ip address]:8181/ user=everyone

2) In Windows Firewall with Advanced Security, create a new inbound rule for port 8181 to allow external connections

3) In applicationhost.config, in the node for your project, add:

<binding protocol="http" bindingInformation="*:8181:[your ip address]" />

Do NOT add (as was suggested in another answer):

<binding protocol="http" bindingInformation="*:8181:*" />

The above wildcard binding broke my access from http://192.168.1.6:8181/

itzmebibin
  • 8,162
  • 6
  • 43
  • 55
Erwin
  • 495
  • 6
  • 8
  • 5
    Also, you must run Visual Studio with Administrator Privilege otherwise you won't be able to bind to IIS Express. When finished testing as Administrator you will need to reverse the previous steps by removing the extra entry from the applicationhost.config file and removing the HTTP.sys entry using: `netsh http delete urlacl url=http://[your ip address]:8181/` – SteveC Dec 30 '18 at 05:51
  • 1
    If your computer is set to a different language, then "user=everyone" should be something else. In French it is user="Tout le monde". See this answer: https://stackoverflow.com/a/18856394/1317559 – Yster Aug 25 '20 at 18:32
  • Thank you, worked great with Parallel Desktop setup. I logged in just to upvote & say thank you! – user3603110 Feb 23 '21 at 11:38
25

So, I solved this by going to my website in IIS Manager and changing the host name in site bindings from localhost to *. Started working immediately.

Site Bindings in IIS

SINGULARITY
  • 889
  • 9
  • 9
10

For Visual Studio 2017 and Visual Studio 2015, IIS Express settings is stored in the hidden .vs directory and the path is something like this .vs\config\applicationhost.config, add binding like below will work

<bindings>
    <binding protocol="http" bindingInformation="*:8802:localhost" />
    <binding protocol="http" bindingInformation="*:8802:127.0.0.1" />
</bindings>

Syntax: https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.administration.binding.bindinginformation?view=iis-dotnet

RJFalconer
  • 8,488
  • 3
  • 44
  • 58
imgen
  • 2,357
  • 6
  • 36
  • 59
  • 2
    For anyone using this method, I't important to note that the correct binding Information template is : ip address:port:host header . So the correct implementation is : ` ` – Lionnel Afangbedjee Aug 01 '18 at 18:41
  • 1
    The .vs folder is the folder in your project root folder. In that applicationhost.config look for the tag – NexX Jun 24 '19 at 11:22
5

Don't forget to bind to the IPv6 address as well! I was trying to add a site on 127.0.0.1 using localhost and got the bad request/invalid hostname error. When I pinged localhost it resolved to ::1 since IPv6 was enabled so I just had to add the additional binding to fix the issue.

IIS Site Bindings

Jeff Camera
  • 5,006
  • 4
  • 40
  • 58
4

This solved my problem (sorry for my bad English):

  1. open cmd as administrator and run command (Without the square brackets):
    netsh http add urlacl url=http://[ip adress]:[port]/ user=everyone

  2. in documents/iisexpress/config/applicationhost.config and in your root project folder in (hidden) folder: .vs/config/applicationhost.config you need add row to "site" tag:
    <binding protocol="http" bindingInformation="*:8080:192.xxx.xxx.xxx" />

  3. open "internet information services (iis) manager"
    (to find it: in search in taskbar write "Turn Window features on or off" and open result and then check the checkbox "internet information service" and install that):

    1. in left screen click: computer-name --> Sites --> Default Web Site and
    2. then click in right screen "Binding"
    3. click Add button
    4. write what you need and press "OK".
  4. open "Windows Firewall With Advanced Security",

    1. in left screen press "Inbound Rules" and then
    2. press in right screen "New Rule..."
    3. check port and press Next,
    4. check TCP and your port and press Next,
    5. check "Allow the connection" and press Next,
    6. check all checkbox and press Next,
    7. write name and press Finish.
  5. done.

Yahoo Serious
  • 3,080
  • 1
  • 29
  • 34
izik f
  • 1,885
  • 1
  • 14
  • 13
3

I'm not sure if this was your problem but for anyone that's trying to access his web application from his machine and having this problem:

Make sure you're connecting to 127.0.0.1 (a.k.a localhost) and not to your external IP address.

Your URL should be something like http://localhost:8181/ or http://127.0.0.1:8181 and not http://YourExternalIPaddress:8181/.


Additional information:
The reason this works is because your firewall may block your own request. It can be a firewall on your OS and it can be (the usual) your router.

When you connect to your external IP address, you connect to you from the internet, as if you were a stranger (or a hacker).
However when you connect to your localhost, you connect locally as yourself and the block is obviously not needed (& avoided altogether).

MasterMastic
  • 19,099
  • 11
  • 59
  • 86
  • I have this issue where when using localhost works but when using the machines local IP Address (does not). Why did you specifically say not to use http://yourIPaddress - why does this not work - I'd like to understand? – Code Uniquely Sep 27 '13 at 07:23
  • 1
    It is absurdly trivial for a "hacker" to change the hostname used to connect to a website. – Arafangion Feb 12 '16 at 00:41
  • @Arafangion Change it where and gain what? – MasterMastic Feb 12 '16 at 05:42
  • @MasterMastic: Eg, on linux, edit the /etc/hosts.conf file. Windows has equivalent. Or even more trivially, just tell curl or wget which hostname to report. Here, you're apparently relying on the "hostname" to block requests from the internet, so what the hacker gains is obviously whatever they need to gain by bypassing your apparent "protection" mechanism. IIS still seems to bind to the port. The correct fix is not to bind to the externally accessible port in the first place. – Arafangion Feb 15 '16 at 03:20
  • @Arafangion Yeah but that's a local redirection. The point here isn't `localhost`, it's what it's referring to (`127.0.0.1`). All I'm saying is connect to that and not to your externally visible IP. There is no reliance on the localhost, it's just used as a synonym. I'll edit my answer to make this clearer. – MasterMastic Feb 15 '16 at 04:46
  • @Arafangion Oh it probably confused you because I didn't mention the "external" part :P you made a good point. Thank you! – MasterMastic Feb 15 '16 at 04:53
2

You can use Visual Studio 2005/2008/2010 CMD tool. Run it as admin, and write

aspnet_regiis -i

At last I can run my app successfully.

phant0m
  • 15,502
  • 4
  • 40
  • 77
FrankFan
  • 57
  • 5
2

If working on local server or you haven't got domain name, delete "Host Name:" field. enter image description here

1

Check your local hosts file (C:\Windows\System32\drivers\etc\hosts for example). In my case I had previously used this to point a URL to a dev box and then forgotten about it. When I then reused the same URL I kept getting Bad Request (Invalid Hostname) because the traffic was going to the wrong server.

robaker
  • 1,028
  • 7
  • 11
1

I got this error when I tried to call a webservice using "localhost". I fixed it by using the actual IP instead (192.168...)

Cosmin
  • 2,192
  • 2
  • 20
  • 21
0

I saw the same error after using msdeploy to copy the application to a new server. It turned out that the bindings were still using the IP address from the previous server. So, double check IP address in the IIS bindings. (Seems obvious after the fact, but did not immediately occur to me to check it).

dan9298
  • 195
  • 1
  • 11
0

Double check the exact URL you're providing. I saw this error when I missed off the route prefix defined in ASP.NET so it didn't know where to route the request.

Taran
  • 9,532
  • 3
  • 37
  • 41
0

Make sure IIS is listening to your port.

In my case this was the issue. So I had to change my port to something else like 8083 and it solved this issue.

Dudi
  • 2,913
  • 1
  • 24
  • 22