我正在用C#编写一个超文本传输协议服务器。
当我尝试执行函数HttpListener.Start()
时,我得到一个HttpListenerException
,说
“拒绝访问”。
当我在windows7的管理模式下运行这个应用程序时,它工作得很好。
我可以让它在没有管理模式的情况下运行吗?如果是,是如何实现的?如果没有,我如何使应用程序在开始运行后更改为管理模式?
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;
}
}
}
}
}
https://stackoverflow.com/questions/4019466
复制相似问题