我正在尝试从ASP.NET MVC控制器向远程URL发送http帖子。 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中运行。是否与localhost和开发ssl证书有关?
这是错误:
System.Net.WebException HResult = 0x80131509 Message =基础连接已关闭:发送时发生意外错误。 Source = Plugin.Payment.Stripe内部异常1:IOException:身份验证失败,因为远程方已关闭传输流。
这是我在我的控制器中使用的代码:
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;