How to solve "The network connection was lost" error it occured when used ModernHttpClient in Xamarin.Forms iOS project?

Pelin Konaray 291 Reputation points
2023-04-26T08:36:32.4133333+00:00

I have a mobile application that I developed with Xamarin Forms. I also have an internal web application that hosts API methods for my mobile application requests. I want to request these API methods through my mobile application. However, since my web application is internal, I need to access API methods in the web application via a tunnel. In order to do this, I integrated Airwatch SDK into my mobile application at the request of our customer. (I only do this on the iOS part of my Xamarin.Forms project.) When I looked at the Airwatch SDK AppTunnel documentation, I saw that I needed to use ModernHttpClient in all HttpClient requests for AppTunnel and I made these changes. I am currently accessing the internal API methods using the AppTunnel feature of the Airwatch SDK. (AppTunnel with Airwatch SDK: https://developer.vmware.com/web/workspace-one/tutorials/Xamarin-iOS/App-Tunneling/Airwatch-SDK) I also have a "Multipart/Form-Data POST" api method that I use for file upload in application. I've changed this to work with ModernHttpClient as well. Uploading small files also worked without a problem. However, I am getting the following error while uploading a large file.

System.AggregateException: One or more errors occurred. (The network connection was lost.) ---> System.Net.WebException: The network connection was lost. ---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=54, NSUnderlyingError=0x28370d980 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=54, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask .<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(\n "LocalDataTask .<1>"\n), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=https://base-url.com/api/v2/filesystem/upload, NSErrorFailingURLKey=https://base-url.com.com/api/v2/filesystem/upload, _kCFStreamErrorDomainKey=1}\n --- End of inner exception stack trace ---\n at ModernHttpClient.NativeMessageHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x1088d8130 + 0x01143> in <67401bece23c4316a0cb1acd7b396157#0821ad7e7f240e13aa5b8f0971c62be4>:0 \n at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) <0x1073d7a80 + 0x005f4> in <992c809de76342e0b46dc1b912da2d7b#0821ad7e7f240e13aa5b8f0971c62be4>:0 \n --- End of inner exception stack trace ---\n at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) <0x10490f1a0 + 0x00048> in <aae03b95719e488f8a163bae4d2b5dcb#0821ad7e7f240e13aa5b8f0971c62be4>:0 \n at System.Threading.Tasks.Task1[TResult].GetResultCore (System.Boolean waitCompletionNotification) <0x10490a080 + 0x00097> in <aae03b95719e488f8a163bae4d2b5dcb#0821ad7e7f240e13aa5b8f0971c62be4>:0 \n at System.Threading.Tasks.Task1[TResult].get_Result () <0x104909fe0 + 0x00067> in <aae03b95719e488f8a163bae4d2b5dcb#0821ad7e7f240e13aa5b8f0971c62be4>:0 \n at AppSDK.V2.Service.Uploader.SendStreamToServer (System.String fileName, System.IO.Stream stream, System.Collections.Specialized.NameValueCollection nvc) <0x107cd73b0 + 0x00633> in <d8bc2b7aa0fc423f94af434e3edd0df7#0821ad7e7f240e13aa5b8f0971c62be4>:0 \n---> (Inner Exception #0) System.Net.WebException: The network connection was lost. ---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1005 \"The network connection was lost.\" UserInfo={_kCFStreamErrorCodeKey=54, NSUnderlyingError=0x28370d980 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 \"(null)\" UserInfo={_kCFStreamErrorCodeKey=54, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <F5B801BE-A8A1-4EBC-89D2-EA1D79B4C72B>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(\n \"LocalDataTask <F5B801BE-A8A1-4EBC-89D2-EA1D79B4C72B>.<1>\"\n), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=https://base-url.com.com/api/v2/filesystem/upload, NSErrorFailingURLKey=https://base-url.com.com/api/v2/filesystem/upload, _kCFStreamErrorDomainKey=1}\n --- End of inner exception stack trace ---\n at ModernHttpClient.NativeMessageHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x1088d8130 + 0x01143> in <67401bece23c4316a0cb1acd7b396157#0821ad7e7f240e13aa5b8f0971c62be4>:0 \n at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) <0x1073d7a80 + 0x005f4> in <992c809de76342e0b46dc1b912da2d7b#0821ad7e7f240e13aa5b8f0971c62be4>:0 <---\n I tried the followings to resolve this error.

    1. Uploading a large file by dividing it into smaller parts (for example, trying to upload an 850 mb file in 500 kb chunks. In this case, for example, 60-70 mb of the file is uploaded, then suddenly I get the above error.)
    1. I increased the timeout of the HttpClient object.
   _client.Timeout = TimeSpan.FromMinutes(20);
    1. I increased the timeout of ModernHttpClient.
   NativeMessageHandler handler = new NativeMessageHandler() 
   { 
       Timeout = TimeSpan.FromMinutes(20) 
   };
   HttpClient _client = new HttpClient(handler);
    1. I added "Connection: Close" to the header of the http request.
   httpClient.DefaultRequestHeaders.Add("Connection", "Close");

or

   httpClient.DefaultRequestHeaders.ConnectionClose = true;

or

   httpClient.DefaultRequestHeaders.ConnectionClose = false;
    1. I added "Connection: Keep-Alive" to the header of the http request and increased the Keep-Alive timeout (for example: "Keep-Alive: 180000")
   httpClient.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
   httpClient.DefaultRequestHeaders.Add("Keep-Alive", "180000");

However, none of them worked. I saw this issue: https://github.com/anaisbetts/ModernHttpClient/issues/204 It says there is a problem with the server. But I don't understand how I need to make a setting on the server to fix this. What can I do to fix this problem I'm facing while uploading high size files?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,336 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.