简体   繁体   中英

C# Content.ReadAsAsync

As a form of study, I'm performing an integration with the Sandbox API of a payment gateway. I am already able to perform the POST for the desired endpoint by passing the information in the Body + Headers. My question would be about using the Content.ReadAsAsync method to get the return of this POST. In response, I get a lot of information about the payment (including a GUID that identifies the transaction identifier).

I'm trying to use Content.ReadAsAsync in the way below but I'm getting a "null" return. Since it's the first time I do something like that, could you give me an idea of ​​how to get the data together?

Thanks in advance for your help.

 var transactionbuilder = new TransactionBuilder("300", customer, payment);         


            client.BaseAddress = new Uri("//ENDPOINT"); /
            client.DefaultRequestHeaders.Add("//HEADER1");
            client.DefaultRequestHeaders.Add("//HEADER2");
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));


            HttpResponseMessage response = await client.PostAsJsonAsync("//RESOURCE", transactionbuilder);
            TransactionResponse exibeResponse = await response.Content.ReadAsAsync<TransactionResponse>();

            Console.WriteLine(
               $"{exibeResponse.AcquirerTransactionId}\n " +
               $"{exibeResponse.AuthorizationCode}\n" +
               $" {exibeResponse.PaymentId}\n" +
               $" {exibeResponse.ProofOfSale}\n " +
               $"{exibeResponse.ProviderReturnCode} \n" +
               $"{exibeResponse.ProviderReturnMessage} \n" +
               $"{exibeResponse.ReasonCode}\n " +
               $"{exibeResponse.ReasonMessage}\n " +
               $"{exibeResponse.ReceivedDate}\n " +
               $"{exibeResponse.Status}"
               );

Just to share, after the talk with @Yogu I solved it with the option below.

HttpResponseMessage response = await client.PostAsJsonAsync("RESOURCE", transactionbuilder); 
            var content = await response.Content.ReadAsStringAsync(); 

            dynamic formattedContent = JsonConvert.DeserializeObject<dynamic>(content); 
            var OrderPaymentId = formattedContent.Payment.PaymentId;
            var OrderInstallments = formattedContent.Payment.Installments;
            var OrderCapturedDate = formattedContent.Payment.CapturedDate;

            Console.WriteLine($"" +
                $"GUID: {OrderPaymentId}\n" +
                $"Quantidade de Parcelas: {OrderInstallments}\n" +
                $"Data de captura: {OrderCapturedDate}");

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