我試圖通過SSL發出請求。證書已經安裝在機器上,它通過瀏覽器工作。錯誤:C#底層連接已關閉:無法建立SSL/TLS安全通道的信任關係
我使用這個請求:
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] data = encoding.GetBytes(request.Content.OuterXml.ToString());
string password = "XXXX";
X509Certificate2 cert = new X509Certificate2("c:\\zzzz.p12", password);
string key = cert.GetPublicKeyString();
string certData = Encoding.ASCII.GetString(cert.Export(X509ContentType.Cert));
Uri uri = new Uri(request.Url);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(uri);
myRequest.Credentials = new NetworkCredential(request.User, request.Password.ToString());
myRequest.Method = "PUT";
myRequest.ContentType = request.ContentType;
myRequest.ContentLength = data.Length;
myRequest.ClientCertificates.Add(cert);
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
System.IO.StreamReader st = new StreamReader(((HttpWebResponse)myRequest.GetResponse()).GetResponseStream());
使用這個代碼,我得到這個錯誤:
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
問題是什麼?
看看這個帖子,MAYB它可以幫助u到 http://stackoverflow.com/questions/703272/could-not-establish-trust-relationship-for-ssl-tls-secure-通道肥皂 – JohnnBlade
謝謝Johnn Blade –
[C#忽略證書錯誤?](http://stackoverflow.com/questions/2675133/c-sharp-ignore-certificate-errors) –