我在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;
}
}
}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个使用 x509 证书进行客户端/服务器身份验证的 http 侦听器。
我的服务器代码如下。
_listener = new HttpListener();
_listener.Prefixes.Add("https://localhost:8006/");
_listener.Start();
HttpListenerContext context = _listener.GetContext();
Run Code Online (Sandbox Code Playgroud)
我的客户端代码如下
string url = "https://localhost:8006/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
var store = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var cert = store.Certificates.Find(X509FindType.FindBySubjectName, "localhost", true);
request.ClientCertificates.Add((X509Certificate)cert[0]);
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, policyErrs) =>
{
return policyErrs == System.Net.Security.SslPolicyErrors.None;
};
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Run Code Online (Sandbox Code Playgroud)
我相信我已经正确配置了一切。没有证书策略错误,我已将 ssl 证书绑定到端口,并且不需要提升权限来运行侦听器。

如果我在代码中或通过 Chrome 发出网络请求,则会收到此错误。我在这里做错了什么?
