tmenier / Flurl

Fluent URL builder and testable HTTP client for .NET
https://flurl.dev
MIT License
4.23k stars 387 forks source link

Getting "Cannot access a closed Stream" when trying to POST from android emulator #755

Closed asusralis closed 1 year ago

asusralis commented 1 year ago

I am using MAUI with an Android emulator and I am trying to send requests to a local asp service. I am able to send GET requests from the emulator without issue, but sending any type of POST gives this error:

Flurl.Http.FlurlHttpException: 'Call failed. Cannot access a closed Stream: POST at System.IO.MemoryStream.EnsureNotClosed() at System.IO.MemoryStream.CopyToAsync(Stream destination, Int32 bufferSize, CancellationToken cancellationToken) at Xamarin.Android.Net.AndroidMessageHandler.WriteRequestContentToOutput(HttpRequestMessage request, HttpURLConnection httpConnection, CancellationToken cancellationToken) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs:line 500 at Xamarin.Android.Net.AndroidMessageHandler.DoProcessRequest(HttpRequestMessage request, URL javaUrl, HttpURLConnection httpConnection, CancellationToken cancellationToken, RequestRedirectionState redirectState) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs:line 575 at Xamarin.Android.Net.AndroidMessageHandler.DoSendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs:line 417 at System.Net.Http.HttpClient.g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken) at Flurl.Http.FlurlRequest.SendAsync(HttpMethod verb, HttpContent content, CancellationToken cancellationToken, HttpCompletionOption completionOption)

I am able to POST to this endpoint in both Postman and Flurl in a basic console program.

 FlurlHttp.ConfigureClient("http://localhost:5160", cli =>
                cli.Settings.HttpClientFactory = new UntrustedCertClientFactory());
 public class UntrustedCertClientFactory : DefaultHttpClientFactory
    {
        public override HttpMessageHandler CreateMessageHandler()
        {
            return new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (_, _, _, _) => true
            };
        }
    }
 var result = await Constants.ApiUrl
                .AppendPathSegment("translate")
                .PostJsonAsync(new TranslateTextRequest()
                {
                    Text = text,
                    TargetLanguage = targetLanguage,
                    OriginalLanguage = originalLanguage,
                }, cancellationToken: cancellationToken)
                .ReceiveJson<string>(); 
asusralis commented 1 year ago

It seems I can't use "UseHttpsRedirection" in my service.