我正在尝试从 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/