diff --git a/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs b/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs index 13112c48fca..6252e39690f 100644 --- a/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs +++ b/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs @@ -245,7 +245,7 @@ string EncodeUrl (Uri url) try { if (Logger.LogNet) Logger.Log (LogLevel.Info, LOG_APP, $" connecting"); - await httpConnection.ConnectAsync ().ConfigureAwait (false); + await Task.WhenAny(httpConnection.ConnectAsync(),Task.Run(()=> { cancellationToken.WaitHandle.WaitOne(); })).ConfigureAwait(false); if (Logger.LogNet) Logger.Log (LogLevel.Info, LOG_APP, $" connected"); } catch (Java.Net.ConnectException ex) { @@ -265,7 +265,16 @@ string EncodeUrl (Uri url) cancellationToken.Register (httpConnection.Disconnect); if (httpConnection.DoOutput) { - await request.Content.CopyToAsync (httpConnection.OutputStream).ConfigureAwait (false); + using (var stream = await request.Content.ReadAsStreamAsync()) + { + await stream.CopyToAsync(httpConnection.OutputStream, 4096, cancellationToken).ConfigureAwait(false); + } + } + + if (cancellationToken.IsCancellationRequested) + { + httpConnection.Disconnect(); + cancellationToken.ThrowIfCancellationRequested(); } var statusCode = await Task.Run (() => (HttpStatusCode)httpConnection.ResponseCode).ConfigureAwait (false);