Alternative to HttpListener?

2020-02-10 03:25发布

问题:

I'm developing an application that is so far using HttpListener to provide a small standalone http server. However, I've recently discovered that HttpListener needs to be run as Administrator, which is not always going to be possible.

What would the best alternative be? I need http GET and POST, both of which are not simply reading/writing files on the filesystem, they need to run custom .Net code.

My research so far has brought up Cassini, but as far as I can tell, I would have to write a custom version. Is there anything else? In partiular something with the same interface as HttpListener, but that does not require Administrator privileges would be amazing!

回答1:

One alternative that I've found is C# Webserver on CodePlex.

"... a flexible http server which can be embedded in any .Net application. It has a modular design where features are added using modules. The server also supports REST and all http verbs ..."

It has an HttpListener class which I imagine is similar to System.Net.HttpListener, but I haven't used either one of them yet so I can't be certain.



回答2:

One solution to this is covered by this other question - you can give yourself permissions to run HttpListener as a non-admin.

You could get the app to be started from a command file that sorts out the permissions and then runs the real app.



回答3:

Like the comment by Will Dean on your post says, you could run the following netsh command:

netsh http add urlacl url=http://+:8346/ user="NTAuthority\Authenticated Users" sddl="D:(A;;GX;;;AU)"

Substitute the 'http://+:8346/' with your value, and this will allow any authenticated user to run the webserver at the target endpoint.



回答4:

Regarding the statement:

I've recently discovered that HttpListener needs to be run as Administrator

That's not entirely true and some of the other answers touch on one of the reasons, but there is another:

  1. Noted by other posters: You can grant permissions to a non-Administrator. Fine, but not great.
  2. You can listen on localhost, even on port 80, without being an admin. Note: I recall that only "localhost" works and not "127.0.0.1"... so be sure that you pass localhost to your Prefixes.Add call.

We're shipping an internal tool that allows developers to run an HTTP-based app host on their PCs and we at first thought using HttpListener would not be possible due to the Admin (or Admin-granted permissions) problem, but then we found that localhost works just fine without being an Admin. It sort of makes sense: listening externally is "dangerous" but listening on the local machine is not quite as dangerous.



回答5:

Ok, so you have a regular desktop app that needs to allow inbound http connections - hmm - won't windows firewall be an issue?

Assuming not, it sounds almost like a webservice - could you go that route - expose the URLs via that? Although my .Net knowledge is not deep enough to know if you still need to run a specific http server to answer requests. Spring.Net is probably worth a look.



回答6:

Nowin is a great lib that can be integrated in Owin as ServerFactory without dependency on HttpListener. Were able to drop in replace Microsoft.Owin.Host.HttpListener lib without a hitch



回答7:

For .NET Core applications there exists a new (cross-platform) HTTP server implemention: Kestrel.