Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient does not decrypt HTTPS without Fiddler

I am not sure if my question is correct, but I couldn't find any relevant information. I am assuming that HttpClient is supposed to decrypt HTTPS responses automatically, but for some reason, it doesn't, except when the Fiddler is running.

HttpClient Client;
HttpResponseMessage response;

using (var request = new HttpRequestMessage(HttpMethod.Get, "https://www.google.com/"))
{
    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));

    response = await Client.SendAsync(request);
    response.EnsureSuccessStatusCode();
}

string sResponse = await response.Content.ReadAsStringAsync();
Log(sResponse); // "\u001f�\b\0\0\0\0\0\u0002��i{۸�0��\u007f\u0005͜c�c�\u0016" etc.

I tried to disable all https-related options in Fiddler and resetting IE proxy options, but it seems not the case.

like image 685
sooqua Avatar asked May 03 '26 17:05

sooqua


1 Answers

It turned out I had the wrong Accept-Encoding header:

Client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");

EDIT:

You CAN use "gzip, deflate" encoding, but you have to specify decompression method in your HttpClientHandler:

HttpClientHandler handler = new HttpClientHandler()
{
    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};

using (var client = new HttpClient(handler))
{
    // your code
}
like image 136
sooqua Avatar answered May 06 '26 07:05

sooqua



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!