Skip to main content
Retain personal agency via direct, action-oriented questions
Source Link
halfer
  • 20.2k
  • 20
  • 110
  • 207

I'm working on a .NET MAUI Android app that sends order data to a Web API using HttpClient. On 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).

    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.

    Avoid duplicate uploads by confirming success before retrying.
  3. Gracefully handle partial responses or dropped connections.

    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? Please help me to find a solution for this issue.

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? Please help me to find a solution for this issue.

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?

Source Link

Android HttpClient "Socket closed" error on slow connections, but server successfully processes request

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? Please help me to find a solution for this issue.