简体   繁体   中英

Restsharp request to Stripe give SendFailure error

I try to use RestSharp to send request to the Stripe API . I build my request, but it can't be send because of an authentification error. The request return a 0 error code.

Here's my code :

    var client = new RestClient("https://api.stripe.com");
    client.Authenticator = new HttpBasicAuthenticator (stripe_key, "");

    var request = new RestRequest("v1/tokens", Method.POST);
    request.AddParameter("card[number]", card.number);
    request.AddParameter("card[exp_month]", card.expMonth);
    request.AddParameter("card[exp_year]", card.expYear);
    request.AddParameter("card[cvc]", card.cvc);

    IRestResponse response = client.Execute(request);

    Log.trace ("Content : " + response.Content);
    Log.trace ("Status code : " + response.StatusCode.ToString ());
    Log.trace ("Error message : " + response.ErrorMessage);

And my output :

Content : 
Status code : 0
Error message : Error getting response stream (Write: The authentication or decryption has failed.): SendFailure

As I said in the comments, I work on Unity 5.3, which use .NET 2.0. This version of .NET seems to have difficulties to validate SSL certificates. This problem forbid SSL connection, that's why I had a "0" response status. To override this validation, I did this :

ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    return true;
}

All certificates will be validated, so be careful... In my case, I communicate with the Stripe API so in my opinion it's ok ! ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM