asp.net 如何使.net的HttpClient使用http 2.0?

iyfjxgzm  于 2022-12-15  发布在  .NET
关注(0)|答案(4)|浏览(263)

我有一asp.net托管在IIS 10(Windows Server 2016)上的www.example.com Web API。当我从Microsoft Edge浏览器对此发出GET请求时,我看到IIS日志中使用了HTTP 2.0

2015-09-20 21:57:59 100.76.48.17 GET /RestController/Native - 443 - 73.181.195.76 HTTP/2.0 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/42.0.2311.135+Safari/537.36+Edge/12.10240 - 200 0 0 7299

但是,当通过.net 4.6 client发出GET请求时,如下所示:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://myapp.cloudapp.net/");

    HttpResponseMessage response = await client.GetAsync("RestController/Native");
    if (response.IsSuccessStatusCode)
    {
        await response.Content.CopyToAsync(new MemoryStream(buffer));
    }
}

我在服务器日志中看到以下HTTP 1.1日志

2015-09-20 20:57:41 100.76.48.17 GET /RestController/Native - 443 - 131.107.160.196 HTTP/1.1 - - 200 0 0 707

如何使.net客户端使用HTTP/2.0?

plicqrtu

plicqrtu1#

1.确保您使用的是最新版本的Windows 10
2.安装WinHttpHandler

Install-Package System.Net.Http.WinHttpHandler

3.扩展WinHttpHandler以添加http2.0支持:

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

4.将上述处理程序传递给HttpClient构造函数

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

q8l4jmvw2#

除了WinHttpHandleras described in Shawinder Sekhon's answer)之外,.NET Core 3.0还在默认的SocketsHttpHandler#30740)中包含HTTP/2支持。由于HTTP/1.1仍然是默认值,因此必须通过设置HttpClient.DefaultRequestVersion来更改默认值,或者必须在每次请求时更改Version。可以在创建请求消息时设置版本:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://myapp.cloudapp.net/");

    HttpResponseMessage response = await client.SendAsync(
        new HttpRequestMessage(HttpMethod.Get, "RestController/Native")
        {
            Version = HttpVersion.Version20,
        });
    if (response.IsSuccessStatusCode)
    {
        await response.Content.CopyToAsync(new MemoryStream(buffer));
    }
}

或者使用自定义HttpMessageHandler,例如:

public class ForceHttp2Handler : DelegatingHandler
{
    public ForceHttp2Handler(HttpMessageHandler innerHandler)
        : base(innerHandler)
    {
    }

    protected override Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Version = HttpVersion.Version20;
        return base.SendAsync(request, cancellationToken);
    }
}

它可以委托给SocketsHttpHandlerWinHttpHandler或任何其他支持HTTP/2的HttpMessageHandler

using (var client = new HttpClient(new ForceHttp2Handler(new SocketsHttpHandler())))
{
    client.BaseAddress = new Uri("https://myapp.cloudapp.net/");

    HttpResponseMessage response = await client.GetAsync("RestController/Native");
    if (response.IsSuccessStatusCode)
    {
        await response.Content.CopyToAsync(new MemoryStream(buffer));
    }
}
esbemjvw

esbemjvw3#

HttpClient还不支持HTTP/2。它将在下一个版本中提供(代码名称KATANA)。下面是link to their source code for the next release
在此之前,您可以实现自己的HttpMessageHandler对象来实现HTTP/2,并将其传递给HttpClient的构造函数(您可能可以使用KATANA的源代码)。

wlwcrazw

wlwcrazw4#

HTTP/2看起来将在.NET 4.6.2的C#客户端调用中得到支持
https://msdn.microsoft.com/en-us/library/ms171868(v=vs.110).aspx

HTTP/2支持(Windows 10)

HTTP/2是HTTP协议的新版本,它提供了更好的连接利用率(客户端和服务器之间的往返次数更少),从而降低用户加载网页的延迟。网页(与服务相反)从HTTP/2中获益最多,因为该协议优化了作为单个体验的一部分而请求的多个工件。HTTP/2支持已在中添加到ASP.NET中。NET Framework 4.6。由于网络功能存在于多个层,因此Windows、IIS和ASP.NET中需要新功能才能启用HTTP/2。您必须在Windows 10上运行才能将HTTP/2与ASP.NET一起使用。
默认情况下,使用System.Net.Http.HttpClient API的Windows 10通用Windows平台(UWP)应用也支持HTTP/2并处于打开状态。

相关问题