![](/img/trans.png)
[英]The request was aborted: Could not create SSL/TLS secure channel. | System.Net.WebException
[英]System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel
我正在使用以下代码从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服务。
您正在向使用SSL / TLS的网站(即以https开头)发送请求。 就像您在评论中提到的那样,您需要将Web客户端配置为使用SSL / TLS。 您需要确定尝试访问的远程服务器是使用SSL还是TLS,然后使用正确的安全模式。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.