gpt4 book ai didi

c# - 系统.Net.WebException : The request was aborted: Could not create SSL/TLS secure channel

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:22 24 4
gpt4 key购买 nike

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

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 服务。

最佳答案

您正在向使用 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;
}
}

关于c# - 系统.Net.WebException : The request was aborted: Could not create SSL/TLS secure channel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46398185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com