我在端口44445上使用了自签名证书。
服务器代码:
static void Main(string[] args)
{
String[] prefixes = { "http://*:44444/", "https://*:44445/" };
var sl = new HttpListener();
foreach (var prefix in prefixes)
{
sl.Prefixes.Add(prefix);
}
sl.Start();
var context = sl.GetContext();
}
客户代码:
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://127.0.0.1:44445/");
X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
var cert = certStore.Certificates.Find(X509FindType.FindByThumbprint, "aa4275780fefffc1e6bde2d3cbe656c9b9067764", false)[0];
request.ClientCertificates.Add(cert);
// encode post data and set up the request
byte[] postDataBytes = Encoding.UTF8.GetBytes("Hello");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postDataBytes, 0, postDataBytes.Length);
requestStream.Close();
String respStr;
HttpWebResponse resp = null;
// get response
resp = (HttpWebResponse)request.GetResponse();
StreamReader responseReader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
respStr = responseReader.ReadToEnd();
resp.Close();
}
当我启动客户端时,连续 流requestStream = request.GetRequestStream(); 我有一个例外:
System.Net.WebException:'基础连接已关闭:无法建立SSL / TLS安全通道的信任关系。' AuthenticationException:根据验证过程,远程证书无效。
如果我浏览到https://127.0.0.1:44445/,则会收到警告,但我可以恢复该请求。
我为什么得到它以及如何解决这个问题?