alexrainman / ModernHttpClient

ModernHttpClient
MIT License
126 stars 28 forks source link

[UWP] InvalidOperationException when EnableUntrustedCertificates = true #23

Closed hig-dev closed 5 years ago

hig-dev commented 5 years ago
System.InvalidOperationException: This instance has already started one or more requests. Properties can only be modified before sending the first request.

   at System.Net.Http.HttpClientHandler.CheckDisposedOrStarted()
   at System.Net.Http.HttpClientHandler.set_ServerCertificateCustomValidationCallback(Func`5 value)
   at ModernHttpClient.NativeMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
   at MyApp.DownloadService.<DownloadLargeFileInChunks>d__17.MoveNext()

Sample code:

_httpClient = new HttpClient(new NativeMessageHandler()
                {
                    Timeout = TimeSpan.FromHours(12),
                    EnableUntrustedCertificates = true,
                    DisableCaching = true
                });
var response = await _httpClient .GetAsync("SOME_VALID_URL", HttpCompletionOption.ResponseHeadersRead, CancellationToken.None);
// Making the second call using the same http client throws the exception.
var response2 = await _httpClient .GetAsync("SOME_VALID_URL_2", HttpCompletionOption.ResponseHeadersRead, CancellationToken.None);

The exception occurs on UWP but not in Android. When EnableUntrustedCertificates = false, the exception doesn't occur.

alexrainman commented 5 years ago

It will be fixed in the next release but support for untrusted certificates will be removed.