1

如何在.Net Framework应用中请求HTTP2站点 - 不忘初心BBQ

 1 year ago
source link: https://www.cnblogs.com/chen943354/p/16454740.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本文的需求背景是对接苹果公司的推送服务(APNS),苹果在安全方面比较积极,已经严格限制API只支持HTTP2。但是我这里的应用目前仍然是.NET Framework平台,所以必须寻找一种解决方案。本文在调研及验证后,将相关资料整理出来供大家参考。

什么是HTTP2及依赖条件

HTTP/2(超文本传输协议第2版,最初命名为HTTP 2.0),简称为h2(基于TLS/1.2或以上版本的加密连接)或h2c(非加密连接)[1],是HTTP协议的的第二个主要版本,使用于全球资讯网。

多数主流浏览器已经在2015年底支持了该协议。[9]此外,根据W3Techs的数据,截至2021年10月,全球有46.5%的网站支持了HTTP/2。

430009-20220707150602847-643109523.png
划重点:HTTP2依赖的TLS协议版本号必须大于等于1.2,因此后文就分别针对Windows系统、Net Framework框架对HTTP2、TLS1.2的支持情况来梳理。

Windows 对HTTP2的支持情况?

调研结论:需要Windows Server 2016或者Windows 10及更高版本。

参考资料:https://docs.microsoft.com/en-us/iis/get-started/whats-new-in-iis-10/http2-on-iis

430009-20220707150725778-1711679365.png

Windows对TLS1.2的支持情况?

结论:Windows 8.1、Windows Server 2012 R2、Windows 10、Windows Server 2016 和更高版本的 Windows 在本机支持 TLS 1.2。

参考资料:https://docs.microsoft.com/zh-cn/mem/configmgr/core/plan-design/security/enable-tls-1-2-client

430009-20220707154306593-1208013060.png
 

.Net Framework对HTTP2的支持情况?

结论:不支持!但是有解决方案:安装nuget包System.Net.Http.WinHttpHandler

参考资料:https://github.com/dotnet/runtime/issues/31217

430009-20220707150802365-1382503568.png

nuget包介绍如图所示:

430009-20220707152506096-1124756520.png

根据简介可以得知:该nuget包是将Windows系统的WinHTTP接口封装,也就是说程序发起HTTP请求是通过Windows实现,而不是HttpClientHandler。而根据前面的调研,Windows对HTTP2和TLS1.2的支持情况已经非常明确。

.Net Framework对TLS1.2的支持情况?

结论:框架.Net Framework 4.6.2及以上支持tls1.2

参考资料:https://docs.microsoft.com/zh-cn/mem/configmgr/core/plan-design/security/enable-tls-1-2-client

430009-20220707154936042-1764801849.png

核心代码片段

现在确保Windows系统能够满足最低要求 ,即可编写测试应用来验证请求。下面是代码片段,我在Windows Server 2016+.NET Framework 4.8环境可以请求成功。

try
{
    string url = "https://api.push.apple.com" + (":443") + "/3/device/" + appleDeviceToken;
    var req = new HttpRequestMessage(HttpMethod.Post, url);
    req.Version = new Version(2, 0);
    req.Content = new JsonContent("{}");
    var resp = httpClient.SendAsync(req).Result;
    string respContent = resp.Content.ReadAsStringAsync().Result;
    this.outputBox.Text += respContent;
}
catch (Exception ex)
{
    this.exceptionBox.Text = ex.ToString();
}

随着各大浏览器支持和苹果的带头效应,HTTP2的应用会越来越广泛,但是规模庞大的.NET Framework应用却也不能为了连接HTTP2就升级到NET Core平台。通过本文提供的方案,可以最小成本的实现.NET Framework应用成功访问HTTP2站点。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK