none
How to use HTTP2 to download compressed content RRS feed

  • Question

  • Hello,

    As I know .NET Core 3 has support for HTTP2, so I want to do some test. The following is my code to download a simple web page. For quite a number of web sites, which support HTTP 2 protocol, the performance is better than HTTP 1.1.

    static async Task Page2_Http_Get(string url1)
    {
        using var client = new HttpClient(new WinHttpHandler(), true);
        var request = new HttpRequestMessage(HttpMethod.Get, url1)
        {
        Version = new Version(2, 0)
        };
        var response = await client.SendAsync(request);
        string html_content = await response.Content.ReadAsStringAsync();
    }

    However, using the old http client, I can handle the compressed content using the following code.

    static async Task Page1_Http_Get(string url1)
    {
        ServicePointManager.Expect100Continue = false;
        ServicePointManager.SecurityProtocol =
    SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
        ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
        HttpClientHandler clientHandler = new HttpClientHandler()
        {
        AllowAutoRedirect = true,
        AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
        };
        using HttpClient client1 = new HttpClient(clientHandler);
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url1);
        var response = await client1.SendAsync(request);
        string html_content = await response.Content.ReadAsStringAsync();
    }

    But for HTTP 2, I can’t find any code sample to add handling of compressed contents. Besides, the old http client can also handle redirect, how I can do this with HTTP 2 client?

    Please advice.

    PS: I am using .NET Core 3.0 on VS 2019 Version 16.3.1 on Windows 10 (1903).

    Thanks,

    Tuesday, October 1, 2019 8:27 PM

Answers

All replies

  • I'd try asking for help over here.

    https://forums.asp.net/1255.aspx/1?ASP+NET+Core

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Wednesday, October 2, 2019 2:17 AM
    Moderator
  • Hello:

    Thanks for your advice, I posted my question in ASP+NET+CORE.

    But I have to wait until someone provide an answer.

    Thanks,

    Wednesday, October 2, 2019 7:00 AM
  • Sounds good, you're welcome.

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Wednesday, October 2, 2019 1:01 PM
    Moderator
  • Hello:

    I posted my questions, but all the answers I got seem not correct.  From my testing, at least for one specific web site, I can get the HTTP2 response in compressed format, but HTTP2 client in C# can't automatically decompress the contents, I have to write the code to decompress it.  But in HTTP1 client, I don't have to write the code myself.

    But that specific is not easy to test, you have to open an account there.  But from my experience, HTTP2 client can't decompress zipped contents, at least to this extent, HTTP2 is not good comparing to HTTP1.

    Saturday, October 19, 2019 9:01 AM
  • I'd suggest replying back to the thread(s) with follow-up questions. For C# you could also try asking here.

    https://forums.asp.net/37.aspx/1?C+

     another place to try;

    https://stackoverflow.com/questions/tagged/asp.net-core

    https://stackoverflow.com/questions/tagged/asp.net+c%23

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    • Marked as answer by zydjohn Monday, October 21, 2019 5:40 PM
    Saturday, October 19, 2019 11:04 AM
    Moderator