c# - 来自 Visual Studio localhost ssl 的 HTTP Post - 基础连接已关闭

标签 c# asp.net-mvc visual-studio ssl stripe-payments

我正在尝试从 ASP.NET MVC Controller 向远程 url 发送 http post。 ASP.NET MVC 应用程序使用 https://localhost:44302/ 在 iisexpress 上的 Visual Studio 2017 中运行.我收到 The underlying connection was closed: An unexpected error occurred on a send.Authentication failed because the remote party has closed the transport stream. 错误。我使用来自 git bash 的 curl 使用相同的参数调用了相同的 url 服务并且它有效。由于某种原因,它无法在 Visual Studio 中运行。与本地主机和开发 ssl 证书有关吗?

这是错误:

System.Net.WebException HResult=0x80131509 Message=The underlying connection was closed: An unexpected error occurred on a send. Source=Plugin.Payment.Stripe Inner Exception 1: IOException: Authentication failed because the remote party has closed the transport stream.

这是我在 Controller 中使用的代码:

WebRequest wrequest = WebRequest.Create("https://connect.stripe.com/oauth/token");
wrequest.UseDefaultCredentials = true;

// Set the Method property of the request to POST.  
wrequest.Method = "POST";
// Create POST data and convert it to a byte array.  
string postData = "client_secret=" + CacheHelper.GetSettingDictionary("StripeApiKey").Value;
postData += "&code=" + code;
postData += "grant_type=authorization_code";

byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.  
wrequest.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.  
wrequest.ContentLength = byteArray.Length;
// Get the request stream.  
Stream dataStream = wrequest.GetRequestStream();

同一个调用在 bash 中使用 curl 时效果很好:

curl -X POST https://connect.stripe.com/oauth/token \
-d client_secret=SECRET_KEY \
-d code=CODE_KEY \
-d grant_type=authorization_code

我已经尝试了这个 link 中的答案但它没有用,所以我认为这可能与 ssl 证书有关

最佳答案

我在通话前添加了这个,现在可以使用了。 This link有了答案。

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

关于c# - 来自 Visual Studio localhost ssl 的 HTTP Post - 基础连接已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52133606/

相关文章:

javascript - 如何使用 Javascript 设置用于打印的 Html 页面宽度和高度

c# - 如何使用 Windows 窗体应用程序检索 ComboBox 中的数据库表?

c# - 使用 ASP.NET MVC 数据类型属性的电子邮件地址验证

asp.net-mvc - 防止asp.net mvc应用程序中的多个POST

c# - 是否有显示哪些方法没有 XML 文档的 Visual Studio 扩展?

c++ - 关于C中的任务递归(Visual Studio 15)

c# - AutoMapper 从 3.3.1 更新到 4.0.4 映射失败

c# - 使用C#控制扬声器音量

visual-studio - 在单个 "Windows Runtime Component C++/WinRT"项目中使用多个 .IDL (MIDL) 文件的正确方法

asp.net-mvc - 从 ASP.NET MVC 开始