【问题标题】:PayPal API, HttpWebRequest throws SSL WebExceptionPayPal API,HttpWebRequest 抛出 SSL WebException
【发布时间】:2017-06-04 13:47:12
【问题描述】:

我正在尝试获取 PayPal 访问令牌,如下所示: https://developer.paypal.com/docs/integration/direct/make-your-first-call/#get-an-access-token

我的 C# 代码如下所示:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.sandbox.paypal.com/v1/oauth2/token");
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(clientId + ":" + clientSecret));
request.Accept = "application/json";
request.Headers.Add("Accept-Language", "en_US");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 10000;

byte[] postBytes = Encoding.ASCII.GetBytes("grant_type=client_credentials");
request.ContentLength = postBytes.Length;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

Stream postStream = request.GetRequestStream(); // ###### Here I get the Exception! ######
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

调用 request.GetRequestStream() 时,我收到带有德语文本“Die Anfrage wurde abgebrochen: Es konnte kein geschützter SSL/TLS-Kanal erstellt werden”的 WebException。这意味着它无法创建安全的 SSL/TLS 通道。

我做错了什么?有人可以帮帮我吗?

谢谢, 沃尔克

【问题讨论】:

    标签: c# ssl paypal httpwebrequest


    【解决方案1】:

    正确,他们不再使用 SSL 3,这就是错误的来源。

    您可以使用 TLS 1.1 和 1.2

    ServicePointManager.SecurityProtocol =  SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    

    【讨论】:

      【解决方案2】:

      我找到了解决办法:

      ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //SecurityProtocolType.Tls12;
      

      也许它也可以帮助其他人......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-23
        • 1970-01-01
        • 2010-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多