views:

3416

answers:

7

I'm using HttpListener to allow a user to set up a proxy on a user-defined port. When I start the HttpListener, I get an exception if the application isn't running under administrator privileges in Vista.

From what I've read, this is expected behavior - administrator privileges are required to start listening on a port. But I'm sure there are ways around this, as I run plenty of programs (like Skype) which listen on a port without requiring elevation to administrator.

Is there a way to do this with HttpListener? If not, can I make other API calls in .NET code to set up the port?

+1  A: 

I've never used an HttpListener, but from your description it sounds more like you want to listen on a regular TCP port, instead of embedding your application into a server URL namespace (which is what HttpListener appears to do). You should be able to use regular socket functions (System.Net.Sockets.TcpListener) to open and listen on a TCP port without requiring administrator privileges. I'm almost certain Skype doesn't use an HttpListener.

Greg Hewgill
Interesting - I think this is the best plan. I'll be processing HTTP requests, so it's tough to give up on the HttpListener, but I think this is the best idea.Two links I've found so far that may help: http://is.gd/3wuXhttp://is.gd/3wv2
Jon Galloway
+2  A: 

In XP, you had to use a command-line (httpcfg) to open up the port first, otherwise it wouldn't work for non-admins.

See here - the page explains the issue, and there is a zip at the bottom to make it usable.

Marc Gravell
+12  A: 

While you can write your own HTTP server using normal TCP/IP (it's relatively simple), it is easier to use HttpListener, which takes advantage of the HTTP.SYS functionality added in Windows XP SP2.

However, HTTP.SYS adds the concept of URL ACLs. This is partly because HTTP.SYS allows you to bind to sub-namespaces on port 80. Using TCP/IP directly avoids this requirement, but means that you can't bind to a port that's already in use.

On Windows XP, you can use the HttpCfg.exe program to set up a URL ACL granting your user account the right to bind to a particular URL. It's in the Platform SDK samples.

On Windows Vista, HTTPCFG is still supported, but the functionality has been absorbed into NETSH:

netsh http show urlacl

...will show a list of existing URL ACLs. The ACLs are expressed in SDDL.

netsh http add urlacl url=http://+:80/MyUri user=DOMAIN\User listen=yes

...will configure the MyURI namespace so that DOMAIN\User can listen to requests.

Roger Lipscombe
wow. you came back after 4 months to edit a single word in your post? impressive, sir :)
Jeff Atwood
A: 

hi..i already tried the netsh command in my vista and have added the port successfully. But after doing it my program still throws "Access is denied" exception. Then I also added the program to be allowed in the firewall Is there anything I should be doing more?

A: 

Thanks guys! i answered my own question. Wow, finally I had the httplistener working!!!

Just run the Visual Studio "Run as Administrator"

A: 

This is very easy to do.

Example: http://forum.skype.com/index.php?showtopic=260361&view=findpost&p=1158301

A: 

Thanks Jeff Atwood, Your answer was very helpful!