c# - Paypal API,HttpWebRequest 抛出 SSL WebException

标签 c# ssl paypal httpwebrequest

我正在尝试获取 PayPal 访问 token ,如下所示: 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 channel 。

我做错了什么?谁能帮帮我?

谢谢, 沃尔克

最佳答案

我找到了解决方案:

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

也许它对其他人也有帮助......

关于c# - Paypal API,HttpWebRequest 抛出 SSL WebException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41748946/

相关文章:

Paypal 错误 : Dispatch Error

php - PayPal IPN 到 mysql 数据库无法正常工作

c# - 推荐用于 Windows 的 Git 工具

c# - 如何处理 System.Drawing.Color <--> System.Windows.Media.Color 模糊引用

python - 为 SocketServer 添加 SSL 支持

facebook - 使用 HTTPS 时为 "Empty response received"

paypal - 使用 paypal 和 NodeJS 进行支付

c# - 字符串数组加字符串不报错,为什么呢?

c# - 使用私有(private)构造函数对类进行单元测试

java - 使用 Java Mail 发送邮件时,远程主机在握手异常期间关闭连接