Paypal 沙箱错误:请求已中止:无法创建 SSL/TLS 安全通道
Error Paypal sandbox: The request was aborted: Could not create SSL/TLS secure channel
如果是沙箱,我会收到错误 "The request was aborted: Could not create SSL/TLS secure channel." 但这适用于我的实时凭据。
这是代码片段
var uri = new Uri(url);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var request = WebRequest.Create(uri);
var encoding = new UTF8Encoding();
var requestData = encoding.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Timeout = (300 * 1000); //TODO: Move timeout to config
request.ContentLength = requestData.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(requestData, 0, requestData.Length);
}
var response = request.GetResponse();
string result;
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
result = reader.ReadToEnd();
}
return result;
问题是 C# 在尝试连接到 PayPal 服务器时默认使用 SSL3,而 PayPal 不接受。 PayPal 至少需要(更安全的)TLS 1.2。
您可以通过以下代码进行设置:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
如果是沙箱,我会收到错误 "The request was aborted: Could not create SSL/TLS secure channel." 但这适用于我的实时凭据。
这是代码片段
var uri = new Uri(url);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var request = WebRequest.Create(uri);
var encoding = new UTF8Encoding();
var requestData = encoding.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Timeout = (300 * 1000); //TODO: Move timeout to config
request.ContentLength = requestData.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(requestData, 0, requestData.Length);
}
var response = request.GetResponse();
string result;
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
result = reader.ReadToEnd();
}
return result;
问题是 C# 在尝试连接到 PayPal 服务器时默认使用 SSL3,而 PayPal 不接受。 PayPal 至少需要(更安全的)TLS 1.2。
您可以通过以下代码进行设置:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;