HttpListener 访问被拒绝 [英] HttpListener Access Denied

查看:63
本文介绍了HttpListener 访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 编写一个 HTTP 服务器.

I am writing an HTTP server in C#.

当我尝试执行函数 HttpListener.Start() 时,我收到一个 HttpListenerException

When I try to execute the function HttpListener.Start() I get an HttpListenerException saying

拒绝访问".

当我在 Windows 7 中以管理员模式运行该应用程序时,它工作正常.

When I run the app in admin mode in windows 7 it works fine.

我可以让它在没有管理员模式的情况下运行吗?如果是,如何?如果不是,我如何在开始运行后将应用程序更改为管理员模式?

Can I make it run without admin mode? if yes how? If not how can I make the app change to admin mode after start running?

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;
                }
            }
        }
    }
}

推荐答案

是的,您可以在非管理员模式下运行 HttpListener.您需要做的就是授予特定 URL 的权限.例如

Yes you can run HttpListener in non-admin mode. All you need to do is grant permissions to the particular URL. e.g.

netsh http add urlacl url=http://+:80/MyUri user=DOMAINuser

文档位于此处.

这篇关于HttpListener 访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆