1

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?

ivpavici
  • 1,117
  • 2
  • 19
  • 30
don great
  • 53
  • 1
  • 11
  • `HttpClient` does not retry, unless you are doing retries in a custom `HttpMessageHandler`. – Stephen Cleary May 18 '17 at 15:34
  • I dont have a HttpMessageHandler implemented. Strange why would this happen. Do you think it has something to do with .Net core? – don great May 18 '17 at 15:56
  • No, `HttpClient` does not automatically retry POSTs on any platform. – Stephen Cleary May 18 '17 at 17:12
  • Weird issue happened to me recently on dotnet 6 blazor. Server requests were slow due to a backup running simultaneously, and request was processed twice. Client side UI doesn't allow this. Looked at server iis logs and it showed two POST requests, one taking 25seconds and another 5 (closer to normal) and finishing at the same exact time. I'm stumped. Did you ever figure this out? – diegorodny Aug 16 '22 at 19:46

0 Answers0