Why HttpListener couldn't start?

I am designing a http server using .NET. I basically use HttpListener to get http request from client. At the beginning, I have to specify the URL, and add that URL to HttpListener. Then, I have to let the HttpListener to start to listen. The problem occurs when it starts to listen. It generate an exception when it starts to listen.

The code is the following:
string url = "http://*:80/"; //where * is the IP address of my PC.
listener = new HttpListener();
listener.Prefixes.Add(url);

listener.Start(); //this is where exception occurs, it couldn't start!!!

My guess is that I have to register that URL using netsh. Is it correct?

Jon Skeet
people
quotationmark

I can think of two simple reasons this might be happening:

  • You have something else which is already listening on port 80 (such as IIS)
  • You don't have permission to listen on port 80

The exception should indicate which of these is the case. It's always important to pay attention to exception messages - a lot of errors can be fixed just by reading exceptions carefully.

It's probably simplest to start off listening on a different port.

EDIT: In running a few experiments I've found:

  • Port 8080 appears to be in use (or at least inaccessible) on my laptop. Using port 8888 works fine though, as does 12345. Just experiment on that front.
  • I can use "localhost" instead of my IP address, but even 127.0.0.1 requires that I run the program as administrator. As administrator I can use any network interface.

people

See more on this question at Stackoverflow