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:
- Detect whether the server successfully processed the request (even if the client didn’t get a full response).
- Avoid duplicate uploads by confirming success before retrying.
- 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?
-
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.H.A.H.– H.A.H.2025年10月29日 08:48:55 +00:00Commented Oct 29 at 8:48