我正在用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;
}
}
}
}
}