嗨,当我向我们的api发出获取请求时,我收到了错误消息“请求已中止:无法创建SSL / TLS安全通道”。
我尝试将ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12添加到我的代码中,因为这是如何解决该问题的最受欢迎的答案。它可以在我的本地主机上运行,但不能在我们的服务器上运行。
我已经使用IISCrypto在我们的服务器上进行了检查,以确保在我们的服务器上启用了TLS 1.2。在Web服务器上的Windows中也启用了“强密码模式”。
string baseURI = "";
string token = "";
ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;
ServicePointManager.ServerCertificateValidationCallback = Validate;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient newClient = new HttpClient();
var authValue = new AuthenticationHeaderValue("Bearer", token);
newClient.DefaultRequestHeaders.Authorization = authValue;
try
{
using (HttpResponseMessage post = await newClient.GetAsync(String.Format("{0}{1}", baseURI, absoluteURI)).ConfigureAwait(false))
{
try
{
post.EnsureSuccessStatusCode();
var read = await post.Content.ReadAsStringAsync().ConfigureAwait(false);
apiPost = post;
apiResponse = read.ToString();
successful = true;
}
catch (HttpRequestException hex)
{
apiResponse = post.StatusCode.ToString();
successful = false;
Email.ErrorEmail("-1", "-1", "Mattermost API Error", hex, apiResponse);
}
}
}
catch (Exception ex)
{
Email.ErrorEmail("-1", "-1", "Mattermost API Error", ex, apiResponse);
}
}
public static bool Validate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors != SslPolicyErrors.None)
{
return false;
}
return true;
}