We are trying to implement a custom (on the settings screen) additional gzip compression in our client that uses HttpClient , so we can register and compare the performance of several different calls over a certain period of time. Our first attempt was to simply conditionally add a header as follows:
HttpRequestMessage request = new HttpRequestMessage(Method, Uri); if (AcceptGzipEncoding) { _client.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip")); }
This created the correct request, but the gzipped response was not unpacked upon return, which resulted in a distorted response. I found that when building the HttpClient we had to enable the HttpClientHandler :
HttpClient _client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip });
This all works well, but we would like to change whether the client sends the Accept-Encoding: gzip header at run time, and there seems to be no way to access or change the HttpClientHandler after passing it to the HttpClient constructor. In addition, changing the headers of the HttpRequestMessage object does not affect the request headers if they are defined by the HttpClientHandler .
Is there a way to do this without recreating the HttpClient every time this changes?
Edit: I also tried changing the reference to the HttpClientHandler to change AutomaticDecompression at runtime, but this is an exception to this exception:
This instance has already launched one or more queries. Properties can only be changed before sending the first request.
pcdev source share