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

标签 c# asp.net paypal-ipn paypal-sandbox

我正在使用 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;

关于c# - Paypal IPN 问题 - 请求被中止 : Could not create SSL/TLS secure channel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35309193/

相关文章:

c# - 对于 Int32 错误,值太大或太小

c# - Paypal IPN 示例代码没有 ActionResult。如果没有 View ,我将如何提供 URL?

c# - 多线程 - 5 或 100 个线程?

c# - 将 XSD 1.1 模式转换为 c# 类

c# - 不实现接口(interface)成员

asp.net - GAC 会修复 ASP.NET/IIS6.0 System.OutOfMemoryException 吗?

php - Paypal响应码——如何正确处理

php - 使用 Paypal IPN 进行类似订阅的购买

c# - 使用 MVVM 时,在代码中的什么位置以及如何告诉文本框显示临时加载消息?

c# - 如何在 Visual Studio 中打开特殊的 Ascii 字符?