Forum Xamarin.Forms

Android - HTTPClient appears to be sending same request multiple times


I've written my first Xamarin application which sends messages from a mobile device back to a central server via a windows service. During development and testing phases we weren't seeing any abnormal behavior or exceptions thrown from the HTTPClient, how ever now the application has gone into a trial phase we're starting too see a couple of concerning behaviors:

1. It appears as though some HTTP Requests are being successfully sent multiple times
When a message is queued on the mobile device it is given a unique sequence number, each message is sent back individually and is only queued once, in the database I am seeing the same message being received multiple times. Each message is only processed once, there is a process in place to recognize when a message is a duplicate, how ever this is not ideal or expected behavior

2. The HTTP client will sporadically throw batches of exceptions
On a few devices everything will appear to be working as expected but then all of a sudden we will receive batches of either
"An error occurred while sending the request at System.Net.Http.HttpClientHandler+d__64.MoveNext ()"
**A task was canceled. at System.Net.Http.HttpClientHandler+d__64.MoveNext () **

These two errors appear to coincide in time so i'm guessing maybe the request is timing out and the client is receiving a false negative response which is kicking off an automatic retry or something inside the HTTP client.

Below is the code used to send the requests

private async Task UploadMessageAsync(Message message)
Uri methodUri = new Uri(MainWebServiceURL + "UploadMessage");
HttpResponseMessage httpResponse;
StreamContent additionalContent = null;

        string responseValue = string.Empty;

        if (IsServiceAvailableAsync().Result)

            using (HttpClientHandler clientHandler = new HttpClientHandler())
                clientHandler.Credentials = GetServiceCredentials();

                using (HttpClient httpClient = new HttpClient(clientHandler))
                    JSONSerialiser serialiser = new JSONSerialiser();
                    MultipartFormDataContent content = new MultipartFormDataContent();
                    StringContent messageContent = new StringContent(serialiser.Serialize(message), UnicodeEncoding.UTF8, serialiser.ContentType);

                    content.Add(messageContent, "MessageContent");

                    additionalContent = GetAdditionalContent(message);

                    if (additionalContent != null)
                        content.Add(additionalContent, "AdditionalContent");

                    httpClient.DefaultRequestHeaders.ExpectContinue = false;
                    httpResponse = await httpClient.PostAsync(methodUri, content);

                    if (httpResponse.IsSuccessStatusCode)
                        responseValue = await httpResponse.Content.ReadAsStringAsync();

        return GetResultObject<T>(responseValue);


Sign In or Register to comment.