本文介绍了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=DOMAIN\user

文档。

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

06-23 01:46