I am working on developing a mobile application that focuses on uploading multiple photos to an api web application. I use Xamarin.Forms and System.Net.Http.HttpClient and Clumsy to simulate bad network conditions (lag, dropped packets, out-of-order packets). The application was originally written using Titanium and worked great for most users, but some users on poor mobile networks often encountered bugs. In the future, we will port Xamarin and try to accommodate users with poor connectivity.
using (var httpClient = CreateClient()) { httpClient.Timeout = TimeSpan.FromMinutes(5); using (var formData = new MultipartFormDataContent()) { // add required fields via formData.Add() ... var httpContent = new ByteArrayContent(imageData); formData.Add(httpContent, "file", Guid.NewGuid() + ".jpg"); try { var response = await httpClient.PostAsync("fileupload", formData).ConfigureAwait(false); if (response.IsSuccessStatusCode) { responseObject = await ResponseMessageToResponseModel(response).ConfigureAwait(false); } } catch (HttpRequestException ex) { Debug.WriteLine("HttpRequestException"); } catch (TaskCanceledException ex) { Debug.WriteLine("TaskCanceledException"); } } }
What I find is that everything works as expected under normal conditions; when you turn on Clumsy with a βdelay, drop out, failβ and try to load PostAsync () never completes and ultimately ends with a TaskCanceledException. The strange thing is that the file gets to the server .. so the POST data apparently went through everything.
I assume that packets dropped in the response from the server mean that the HttpClient never receives the proper response and continues to wait alone until the time runs out.
To understand this, I wonder if anyone has any ideas on how to make this process as valid as possible. Just catching a timeout and retrying is not very good if the file did this for the first time. Any thoughts?
Also, any information on how HttpClient handles dropped / invalid packets, so I can better understand what will happen in this case too.
source share