I am using http client to post payment data to spreedly and charge a credit card. The problem that I am facing is that in some cases its retrying the request second time after 3 secs. The first request has been successful and the card was charged but after 3 secs the httpclient again executes the postasync()
. I am sure that its not because of duplicate requests from browser because I have logging enabled and the log file only shows logs after the Httpclient
code which means that there was no second request from the browser or it would have been logged. Following is the code
var client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes(_spreedly.API + ":api_key");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
// Get the response.
HttpResponseMessage response = await client.PostAsync(
"https://core.spreedly.com/v1/gateways/" + Gateway + "/purchase.json",
new StringContent(postBody, Encoding.UTF8, "application/json"));
// Get the response content.
HttpContent responseContent = response.Content;
string resp = "";
// Get the stream of the content.
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
// Write the output.
resp += await reader.ReadToEndAsync();
}
_logger.LogInformation("Spreedly response for user - {Email} - Response: {Response}", User.Identity.Name, resp);
I can see that the log file shows this log "Spreedly response for user" twice after 3 secs. And 2 responses from spreedly are there with different token. Which shows that the transaction was generated twice by HttpClient
.
Do you think it is retrying the request or something?