B
B
BuBux2021-05-07 12:44:09
ASP.NET
BuBux, 2021-05-07 12:44:09

ASP.NET curl request with data error?

Without data, requests are sent correctly. I sent the same curl request using the php language - everything works correctly. On the client there is a normal post-request.

[WebMethod]
        public static string AuthCaptureBlueSnap()
        {
            string amount = "10.00";
            string currency = "USD";
            string token = "my_token";
            
            string url = "https://sandbox.bluesnap.com/services/2/transactions";
            string data = "{\"cardTransactionType\":\"AUTH_CAPTURE\", \"amount\":" + amount + ", \"currency\":" + currency + ", \"pfToken\":" + token + "}";
            string userName = "user_name";
            string password = "passowrd";
            string usernamePassword = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{userName}:{password}"));

            try
            {
                WebRequest myReq = WebRequest.Create(url);
                myReq.Method = "POST";
                myReq.ContentType = "application/json";
                myReq.ContentLength = data.Length;
                myReq.Headers.Add("Authorization", "Basic " + usernamePassword);
                UTF8Encoding enc = new UTF8Encoding();

                using (Stream ds = myReq.GetRequestStream())
                {
                    ds.Write(enc.GetBytes(data), 0, data.Length);
                }

                WebResponse wr = myReq.GetResponse();
                Stream receiveStream = wr.GetResponseStream();
                StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
                string content = reader.ReadToEnd();

                WebHeaderCollection key = wr.Headers;
                Dictionary<string, string> response = new Dictionary<string, string>();

                for (int i = 0; i < key.Count; ++i)
                {
                    string header = key.GetKey(i);
                    string value = key.Get(header);

                    response.Add(header, value);
                }

                return JsonConvert.SerializeObject(response);
            }
            catch (WebException ex)
            {
                WebExceptionStatus status = ex.Status;
                WebResponse message = ex.Response;

                HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;

                Dictionary<int, string> response = new Dictionary<int, string>();
                response.Add((int)httpResponse.StatusCode, httpResponse.StatusDescription);

                return JsonConvert.SerializeObject(status);
            }
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question