我在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;
}
}
}
} …Run Code Online (Sandbox Code Playgroud)