I'm trying to do a http post to remote url from an ASP.NET MVC controller. The ASP.NET MVC application is running in Visual Studio 2017 on iisexpress using https://localhost:44302/. I'm getting The underlying connection was closed: An unexpected error occurred on a send.
and Authentication failed because the remote party has closed the transport stream.
errors. I've called the same url service with the same parameters using curl from git bash and it works. For some reason it's not working from Visual Studio. Is it something to do with the localhost and the development ssl certificate?
This is the error:
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.
This is the code I'm using in my 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();
The same call works fine with curl from bash:
curl -X POST https://connect.stripe.com/oauth/token \
-d client_secret=SECRET_KEY \
-d code=CODE_KEY \
-d grant_type=authorization_code
I've tried the answer in this link but it didn't work so I think it might have to do with the ssl cert
I added this before the call and it works now. This link had the answer.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
User contributions licensed under CC BY-SA 3.0