我在C#中编写HTTP服务器 .
当我尝试执行函数 HttpListener.Start()
时,我得到 HttpListenerException
说
“访问被拒绝” .
当我在Windows 7中以管理模式运行应用程序时,它工作正常 .
我可以在没有管理员模式的情况下运行吗如果有,怎么样?如果不是,如何在开始运行后让应用程序更改为管理模式?
using System;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
private HttpListener httpListener = null;
static void Main(string[] args)
{
Program p = new Program();
p.Server();
}
public void Server()
{
this.httpListener = new HttpListener();
if (httpListener.IsListening)
throw new InvalidOperationException("Server is currently running.");
httpListener.Prefixes.Clear();
httpListener.Prefixes.Add("http://*:4444/");
try
{
httpListener.Start(); //Throws Exception
}
catch (HttpListenerException ex)
{
if (ex.Message.Contains("Access is denied"))
{
return;
}
else
{
throw;
}
}
}
}
}
10 回答
我也遇到了类似的问题 . 如果你已经保留了url,那么你必须先删除url以非管理员模式运行,否则它将失败,Access is Denied错误 .
是的,您可以在非管理员模式下运行HttpListener . 您需要做的就是为特定URL授予权限 . 例如
文档是here .
你不能,它必须以提升的特权开始 . 您可以使用
runas
动词重新启动它,这将提示用户切换到管理模式编辑:实际上,那不是真的; HttpListener可以在没有提升权限的情况下运行,但您需要为要监听的URL授予权限 . 有关详细信息,请参阅Darrel Miller's answer .
如果使用
http://localhost:80/
作为前缀,则可以在不需要管理权限的情况下侦听http请求 .语法对我来说是错误的,你必须包括引号:
否则我收到“参数不正确”
如果您想使用“user = Everyone”标志,您需要将其调整为您的系统语言 . 在英语中如上所述:
在德国,它将是:
作为不需要提升或netsh的替代方法,您也可以使用TcpListener .
以下是此示例的修改摘录:https://github.com/googlesamples/oauth-apps-for-windows/tree/master/OAuthDesktopApp
读写方法是:
如果将Application Manifest添加到项目中,则可以以管理员身份启动应用程序 .
只需将新项添加到项目中,然后选择"Application Manifest File" . 将
<requestedExecutionLevel>
元素更改为:默认情况下,Windows定义以下每个人都可用的前缀:http://+:80/Temporary_Listen_Addresses/
所以你可以通过以下方式注册你的
HttpListener
:Prefixes.Add("http://+:80/Temporary_Listen_Addresses/" + Guid.NewGuid().ToString("D") + "/";
这有时会导致Skype等软件出现问题,默认情况下会尝试使用端口80 .
您使用“*”,因此您执行以下cmd作为管理员
没用,必须使用*,因为你的规格*:4444~ .
https://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx