HttpWebRequest:请求被中止:无法创建SSL/TLS安全通道

5
我正在制作一个asp.net web forms应用程序,用于使用PayPal进行付款。该应用程序应该使用ssl。当我运行我的应用程序时,一切都很顺利,直到我选择我的PayPal支付按钮。当我按下此按钮时,会出现以下错误:
“请求被中止:无法创建SSL/TLS安全通道。”
描述:在当前Web请求的执行期间发生未处理的异常。请查看堆栈跟踪以获取有关错误和错误源的更多信息。
异常详细信息:System.Net.WebException:请求已中止:无法创建SSL/TLS安全通道。
源代码错误:
Line 203: Line 204: //Retrieve the Response returned from the NVP API call to PayPal. Line 205: HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); Line 206: string result; Line 207: using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
以下是出现错误的方法:
public string HttpCall(string NvpRequest)
{
    string url = pEndPointURL;

    string strPost = NvpRequest + "&" + buildCredentialsNVPString();
    strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode);

    HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
    objRequest.Timeout = Timeout;
    objRequest.Method = "POST";
    //objRequest.ContentLength = strPost.Length;

    try
    {
        using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
        {
            myWriter.Write(strPost);
        }
    }
    catch (Exception)
    {
        // No logging for this tutorial.
    }

    //Retrieve the Response returned from the NVP API call to PayPal.
    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
    string result;
    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
    {
        result = sr.ReadToEnd();
    }

    return result;
}

你安装了调用端点所需的 SSL 证书吗? - Elmar
我相信当 Windows 询问我:“您是否希望信任 IIS Express SSL 证书并继续?”时,我已经回答了“是”,然后又被问到:“您是否要安装此证书?”我也回答了“是”。 - WillemKoonings
我所指的是您正在尝试创建Web请求的终结点。当您使用浏览器浏览该服务端点时,它是否向您呈现了一个证书,您已经在工作站上本地安装/信任了它? - Elmar
当我将URL复制到浏览器中时,我会收到以下消息:ACK = Failure&L_ERRORCODE0 = 81002&L_SHORTMESSAGE0 =未指定方法&L_LONGMESSAGE0 =不支持指定的方法&L_SEVERITYCODE0 =错误。这是您问题的答案吗?我以前从未使用过SSL,所以如果我不清楚,我很抱歉。 - WillemKoonings
1
你的代码片段似乎没有指定要使用哪个安全协议- 例如:ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 这里有一个相关的主题,我认为你也需要查看problems-with-paypal-api-http-call - Elmar
是的,谢谢您。在指定tls12之后,我不再遇到错误了。 - WillemKoonings
1个回答

13
你的代码片段似乎没有明确指定要使用的安全协议 -
示例:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

在研究与PayPal API不同的身份验证方法后,我发现了这个。

这里有一个相关话题值得一提。problems-with-paypal-api-http-call

注意:这个答案是在原始问题的一系列评论之后添加的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接