【问题标题】:System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channelSystem.Net.WebException:请求被中止:无法创建 SSL/TLS 安全通道
【发布时间】:2017-09-25 05:31:47
【问题描述】:

我正在使用以下代码从我的 WCF Web 服务发送推送通知

using (var wc = new WebClient())
{
    wc.Headers.Add("Authorization", "Key=" + ConfigurationSettings.AppSettings["apiKey"]);
    var nameValues = new NameValueCollection
    {
        {"registration_id", objSendNotificationBE.RegistrationID},
        {"collapse_key", Guid.NewGuid().ToString()},
        {"data.payload", objSendNotificationBE.Message}
    };
    var resp = wc.UploadValues("https://android.googleapis.com/gcm/send", nameValues);
    respMessage = Encoding.Default.GetString(resp);
    if (respMessage == "Error=InvalidRegistration")
    {
        string respMessage = "";
    }
}

它工作正常,但有时我会遇到异常

System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
   at System.Net.WebClient.UploadValues(Uri address, String method, NameValueCollection data)
   at System.Net.WebClient.UploadValues(String address, NameValueCollection data)

我已经在 azure 服务器上部署了我的 Web 服务。

【问题讨论】:

  • 你能正确格式化你的代码吗?目前很难阅读
  • 尝试添加ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;使用前 (var wc = new WebClient())
  • 尊敬的 Jitenderthanx 的回复,但正如我提到的,这个问题有时会不定期出现,我如何检查为什么它只发生在某个时间而不是针对所有请求。

标签: c# wcf ssl


【解决方案1】:

您正在向使用 SSL/TLS 的站点发送请求,即以 https 开头。就像您在评论中提到的那样,您需要将 Web 客户端配置为使用 SSL/TLS。您需要确定您尝试访问的远程服务器是使用 SSL 还是 TLS,然后使用正确的安全模式。

你可以在this question.看到这个回答

using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

class Program
{
    static void Main(string[] args)
    {
        Uri address = new Uri("https://archive.org/details/OTRR_In_The_Name_Of_The_Law_Singles");

        ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 ;

        using (WebClient webClient = new WebClient())
        {
            var stream = webClient.OpenRead(address);
            using (StreamReader sr =new StreamReader(stream))
            {
                var page = sr.ReadToEnd();
            }
        }
    }

    /// <summary>
    /// Certificate validation callback.
    /// </summary>
    private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
    {
        // If the certificate is a valid, signed certificate, return true.
        if (error == System.Net.Security.SslPolicyErrors.None)
        {
            return true;
        }

        Console.WriteLine("X509Certificate [{0}] Policy Error: '{1}'",
            cert.Subject,
            error.ToString());

        return false;
    }
}

【讨论】:

  • 我通过添加这两行来解决这个问题 ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 |安全协议类型.Tls;但我不明白为什么这个错误会在一段时间内出现,而不是每次我们调用 Web 服务时。
猜你喜欢
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
  • 2018-04-22
  • 2012-10-30
相关资源
最近更新 更多