简体   繁体   English

Paypal通过C#中的400错误请求错误保存了卡休息API

[英]Paypal saved card rest API through 400 Bad Request error in C#

I am developing application using .net MVC C#. 我正在使用.net MVC C#开发应用程序。 I tried to call rest API of PayPal for save credit card details, my code were working fine but suddenly it starting through 400 Bad Request exception. 我尝试调用PayPal的rest API来保存信用卡详细信息,我的代码工作正常,但突然它从400 Bad Request异常开始。 here is my code, 这是我的代码,

private static async Task<string> StorePaymentCards(string accessToken, PaymentInfoModel cardDetails)
    {
        try
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.sandbox.paypal.com/v1/vault/credit-card");
            var result = "";
            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.Method = "POST";
            httpWebRequest.Accept = "application/json; charset=utf-8";
            httpWebRequest.Headers.Add("Authorization", "Bearer " + accessToken);

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                var loginjson = new JavaScriptSerializer().Serialize(new
                {
                    payer_id = cardDetails.PayerId.Trim(),
                    type = cardDetails.CardType.Trim(),
                    number = cardDetails.CardNumber.Trim(),
                    expire_month = cardDetails.ExpireMonth.Trim(),
                    expire_year = cardDetails.ExpireYear.Trim(),
                    first_name = cardDetails.FirstName.Trim()
                });

                streamWriter.Write(loginjson);
                streamWriter.Flush();
                streamWriter.Close();

                //The code fails when creating the Response here, and go into catch block
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }
                return result;
            }

        }

        catch (Exception ex)
        {
            return ex.GetBaseException().Message;
        }
    }
}

Can anyone help me out from this error? 谁能帮我解决这个错误? Thank you in advance. 先感谢您。

 private static async Task<string> StorePaymentCards(string accessToken, PaymentInfoModel cardDetails)
    {
        try
        {
          //my stuff
        }
        catch (WebException ex)
        {
            string text = null;
            using (WebResponse response = ex.Response)
            {
                HttpWebResponse httpResponse = (HttpWebResponse)response;
                Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                using (Stream data = response.GetResponseStream())
                using (var reader = new StreamReader(data))
                {
                    text = reader.ReadToEnd();
                    Console.WriteLine(text);
                }
            }
            return text; //ex.GetBaseException().Message;
        }
    }

This is changed I have done in my catch block to trace proper error, so it return me that error which I was facing. 我在catch块中所做的更改已更改为跟踪正确的错误,因此它将我遇到的错误返回给我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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