HttpListener server returns an error: (503) Servic

2019-06-12 16:38发布

I am trying to create a http listener that uses x509 certificates for client/server authentication.

My server code is as follows.

_listener = new HttpListener();
_listener.Prefixes.Add("https://localhost:8006/");
_listener.Start();
HttpListenerContext context = _listener.GetContext();

My client code is as follows

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();

I believe I have configured everything correctly. There are no certificate policy errors, I have bound the ssl cert to the port, and require no elevated permissions to run the listener.

enter image description here

If I make a web request in code or through Chrome I get this error. What am I doing wrong here?

enter image description here

1条回答
一纸荒年 Trace。
2楼-- · 2019-06-12 17:27

Turns out the prefix was incorrect, once I changed 'localhost' to '+' everything worked fine.

查看更多
登录 后发表回答