Wir können keine Verbindung zum https Server mit webrequest
oder htmlagilitypack
The underlying connection was closed: An unexpected error occurred on a receive.System.Net.WebException:
could not create SSL/TLS secure channel on server
Unser Code funktioniert gut auf localhost und wir haben auch den folgenden Teil in meiner Code-Datei hinzugefügt, aber wir können nicht herausfinden, warum es nur auf dem Server passiert.
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
Wenn jemand eine Idee dazu hat, dann teilen Sie uns bitte mit.
Manchmal ist es so, weil die Webanfrage keine selbstsignierten Zertifikate akzeptiert. Ich habe diese Singleton-Klasse, die ich normalerweise verwende. Es akzeptiert alle selbst signierten Zertifikate. Es wäre einfacher festzustellen, ob es eine einfachere Lösung gibt, wenn Sie die URL teilen, auf die Sie zugreifen möchten.
public sealed class Certificates
{
private static Certificates instance = null;
private static readonly object padlock = new object();
Certificates()
{
}
public static Certificates Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new Certificates();
}
return instance;
}
}
}
public void GetCertificatesAutomatically()
{
ServicePointManager.ServerCertificateValidationCallback +=
new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors)
=> { return true; });
}
private static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
//Return true if the server certificate is ok
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
bool acceptCertificate = true;
string msg = "The server could not be validated for the following reason(s):\r\n";
//The server did not present a certificate
if ((sslPolicyErrors &
SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable)
{
msg = msg + "\r\n -The server did not present a certificate.\r\n";
acceptCertificate = false;
}
else
{
//The certificate does not match the server name
if ((sslPolicyErrors &
SslPolicyErrors.RemoteCertificateNameMismatch) == SslPolicyErrors.RemoteCertificateNameMismatch)
{
msg = msg + "\r\n -The certificate name does not match the authenticated name.\r\n";
acceptCertificate = false;
}
//There is some other problem with the certificate
if ((sslPolicyErrors &
SslPolicyErrors.RemoteCertificateChainErrors) == SslPolicyErrors.RemoteCertificateChainErrors)
{
foreach (X509ChainStatus item in chain.ChainStatus)
{
if (item.Status != X509ChainStatusFlags.RevocationStatusUnknown &&
item.Status != X509ChainStatusFlags.OfflineRevocation)
break;
if (item.Status != X509ChainStatusFlags.NoError)
{
msg = msg + "\r\n -" + item.StatusInformation;
acceptCertificate = false;
}
}
}
}
//If Validation failed, present message box
if (acceptCertificate == false)
{
msg = msg + "\r\nDo you wish to override the security check?";
// if (MessageBox.Show(msg, "Security Alert: Server could not be validated",
// MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
acceptCertificate = true;
}
return acceptCertificate;
}
}
Rufen Sie einfach die Methode auf, bevor Sie eine Web-Anfrage machen.
Certificates.Instance.GetCertificatesAutomatically();
Es würde auch helfen, das Problem zu diagnostizieren, wenn wir (den Code) sehen könnten, wie Sie Ihre Webanfrage machen.