1

I'm working on a .NET MAUI Android app that sends order data to a Web API using HttpClient. On normal Wi-Fi connections everything works fine, but when I throttle the network speed to around 56 kbps (GPRS/HSCSD) for testing, I sometimes get this exception:

System.Net.Sockets.SocketException: Socket closed

The strange part is the order is actually saved successfully on the server, but the client still throws the exception. So the upload logic fails locally even though the backend already processed the request.

Here’s a simplified version of my code:

public async Task<bool> LocalOrderCompleteSavingRequest(PosSavingOrder requestModel)
{
 using var httpClient = new HttpClient();
 httpClient.DefaultRequestHeaders.Accept.Clear();
 httpClient.DefaultRequestHeaders.Accept.Add(
 new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
 httpClient.DefaultRequestHeaders.Authorization =
 new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "<token>");
 httpClient.DefaultRequestHeaders.Add("IntegrationID", "<guid>");
 httpClient.Timeout = TimeSpan.FromSeconds(120); // tried increasing, still fails
 try
 {
 var jsonContent = JsonSerializer.Serialize(requestModel);
 var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
 var response = await httpClient.PostAsync("<api-base-url>", content);
 if (response.IsSuccessStatusCode)
 return true;
 return false;
 }
 catch (Exception ex)
 {
 Console.WriteLine(ex);
 return false;
 }
}

When running on a very slow network, the exception occurs right after the upload completes, and the log shows "Socket closed". The API still receives and processes the POST request successfully.

I want to:

  1. Detect whether the server successfully processed the request (even if the client didn’t get a full response).
  2. Avoid duplicate uploads by confirming success before retrying.
  3. Gracefully handle partial responses or dropped connections.

Is there any way in .NET (MAUI / Android) to detect whether the request body was fully sent and server responded before socket closed?

halfer
20.2k19 gold badges110 silver badges207 bronze badges
asked Oct 24 at 14:00
1
  • Adding a GUID/UUID (or whatever it is called today) to every operation is what I do. On the client side I retry (repeating with the same GUID) often with a prompt to the user (but sometimes not). On the server side I store this GUID and respond to the client , when I receive duplicate. It is not like it is in the textbooks, but I do it since the time you didn't have to throttle your connection speed to have it that slow, and it works. Commented Oct 29 at 8:48

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.