How to make the .net HttpClient use http 2.0?

前端 未结 4 1512
一生所求
一生所求 2020-12-05 05:30

I have an asp.net web api hosted on IIS 10 (windows server 2016). When I make a GET request to this from a Microsoft Edge browser, I see that HTTP 2.0

4条回答
  •  有刺的猬
    2020-12-05 05:50

    1.Make sure you are on the latest version of Windows 10.

    2.Install WinHttpHandler:

    Install-Package System.Net.Http.WinHttpHandler
    

    3.Extend WinHttpHandler to add http2.0 support:

    public class Http2CustomHandler : WinHttpHandler
    {
        protected override Task SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            request.Version = new Version("2.0");
            return base.SendAsync(request, cancellationToken);
        }
    }
    

    4.Pass above handler to the HttpClient constructor

    using (var httpClient = new HttpClient(new Http2CustomHandler()))
    {
          // your custom code
    }
    

提交回复
热议问题