Skip to main content
 首页 » 编程设计

c#之Paypal IPN 问题之请求被中止 : Could not create SSL/TLS secure channel

2024年02月05日12cyq1162

我正在使用 paypal sandox 来测试支付网关。问题出在支付成功后调用的IPN代码。我正在使用的代码运行良好,但自上个月以来突然停止工作。我不确定发生了什么,它与 paypal 在其 github 存储库中提供的代码完全相同。有人可以指导我正确的方向吗?

这是我用于 IPN Controller 的代码

public ActionResult Index() 
{ 
    //Post back to either sandbox or live 
    string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr"; 
    //string strLive = "https://www.paypal.com/cgi-bin/webscr"; 
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox); 
 
    //Set values for the request back 
    req.Method = "POST"; 
    req.ContentType = "application/x-www-form-urlencoded"; 
    byte[] Param = Request.BinaryRead(System.Web.HttpContext.Current.Request.ContentLength); 
    string strRequest = Encoding.ASCII.GetString(Param); 
    strRequest = strRequest + "&cmd=_notify-validate"; 
    req.ContentLength = strRequest.Length; 
 
    //Send the request to PayPal and get the response 
    StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII); 
    streamOut.Write(strRequest); 
    streamOut.Close(); 
    StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream()); 
    string strResponse = streamIn.ReadToEnd(); 
    streamIn.Close(); 
 
    if (strResponse == "VERIFIED") 
    { 
        //check the payment_status is Completed 
        //check that txn_id has not been previously processed 
        //check that receiver_email is your Primary PayPal email 
        //check that payment_amount/payment_currency are correct 
        //process payment 
    } 
    else if (strResponse == "INVALID") 
    { 
        //log for manual investigation 
    } 
    else 
    { 
        //Response wasn't VERIFIED or INVALID, log for manual investigation 
    } 
    return View(); 
} 

错误信息是这样的:

The request was aborted: Could not create SSL/TLS secure channel.

它在这条线上中断

 //Send the request to PayPal and get the response 
 StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII); 

我已经搜索了与此错误相关的现有帖子,但它们的日期已经很久了。但是从上个月开始我就开始遇到这个错误。所以,可能是 Paypal 的一些重大变化,我不知道。有人可以帮我吗?非常感谢。

请您参考如下方法:

在您的行上方添加以下内容:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox); 

我一直在使用 PayPal(NVP/签名)Express Checkout 集成,但遇到了这个 SSL/TLS 错误。

我所做的一切似乎都无法解决,但后来我发现将以下代码添加到我的请求之上。作为引用,我使用的是 MVC3/.NET 4,因此默认情况下 Tls1.2 对我不可用(就像在 .NET 4.5 + 中一样)。这段代码的前三行解决了这个问题。我希望它能帮助人们!

ServicePointManager.Expect100Continue = true; 
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; 
ServicePointManager.DefaultConnectionLimit = 9999;