3 回答

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊
設(shè)置安全協(xié)議類型需要在創(chuàng)建發(fā)布請(qǐng)求之前完成。所以這:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
應(yīng)該出現(xiàn)在這之前:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
因此,如果您看到它對(duì)后續(xù)請(qǐng)求有效,則可能是您設(shè)置協(xié)議的時(shí)間太晚了。

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
以下代碼可用于幫助解決問題。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback += ValidateServerCertificate;
...
private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// If the certificate is a valid, signed certificate, return true to short circuit any add'l processing.
if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}
else
{
// cast cert as v2 in order to expose thumbprint prop - if needed
var requestCertificate = (X509Certificate2)certificate;
// init string builder for creating a long log entry
var logEntry = new StringBuilder();
// capture initial info for the log entry
logEntry.AppendFormat("SSL Policy Error(s): {0} - Cert Issuer: {1} - SubjectName: {2}",
sslPolicyErrors.ToString(),
requestCertificate.Issuer,
requestCertificate.SubjectName.Name);
// check for other error types as needed
if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors) //Root CA problem
{
// check chain status and log
if (chain != null && chain.ChainStatus != null)
{
// check errors in chain and add to log entry
foreach (var chainStatus in chain.ChainStatus)
{
logEntry.AppendFormat("|Chain Status: {0} - {1}", chainStatus.Status.ToString(), chainStatus.StatusInformation.Trim());
}
}
}
// replace with your logger
MyLogger.Info(logEntry.ToString().Trim());
}
return false;
}

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
對(duì)于運(yùn)行 .NET 版本 4 的用戶,他們可以使用下面的
ServicePointManager.SecurityProtocol = CType(768, SecurityProtocolType) Or CType(3072,SecurityProtocolType)
ServicePointManager.Expect100Continue = True
- 3 回答
- 0 關(guān)注
- 420 瀏覽
添加回答
舉報(bào)