.net – 为什么在尝试使用HttpListener时会出现“AccessDenied”?

参见英文答案 > HttpListener Access Denied10个
赢7和VS2010 B2.我正在尝试使用内置的HttpListener编写一个最小的Web服务器.但是,我一直收到AccessDenied异常.这是代码
int Run(string[] args) {

        _server = new HttpListener();
        _server.Prefixes.Add("http://*:9669/");
        _server.Start();

        Console.WriteLine("Server bound to: {0}",_server.Prefixes.First());

        _server.BeginGetContext(HandleContext,null);
    }

如果我绑定到系统端口,我可以理解需要以管理员身份运行,但我不明白为什么我对9669的绑定应该需要特殊权限.

有任何想法吗?

解决方法

感谢这个问题: Can I listen on a port (using HttpListener or other .NET code) on Vista without requiring administrator priveleges?

我有一个答案.

netsh http add urlacl url=http://*:9669/ user=fak listen=yes

疯.这是我修改过的功能

int Run(string[] args) {

        var prefix = "http://*:9669/";
        var username = Environment.GetEnvironmentvariable("USERNAME");
        var userdomain = Environment.GetEnvironmentvariable("USERDOMAIN");

        _server = new HttpListener();
        _server.Prefixes.Add(prefix);

        try {
            _server.Start();
        }
        catch (HttpListenerException ex) {
            if (ex.ErrorCode == 5) {
                Console.WriteLine("You need to run the following command:");
                Console.WriteLine("  netsh http add urlacl url={0} user={1}\\{2} listen=yes",prefix,userdomain,username);
                return -1;
            }
            else {
                throw;
            }
        }


        Console.WriteLine("Server bound to: {0}",null);
    }

相关文章

vue阻止冒泡事件 阻止点击事件的执行 <div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些