首页 > 解决方案 > HttpClient HTTP/2 从 Azure Function 调用 APN

问题描述

我正在使用 HttpClient 将 Apple 推送通知发送到 APN HTTP/2 服务器。我已经设法通过使用System.Net.Http.WinHttpHandlernuget 包让它工作:

var http = new HttpClient(new WinHttpHandler());
var request = new HttpRequestMessage(HttpMethod.Post, new Uri(url))
{
    Version = new Version(2, 0),
    Content = new StringContent(json)
};
// Set headers... Send... Read response... HTTP 200 OK

这在 Windows 上运行良好。问题是,其他任何地方都不支持 WinHttpHandler。例如,我无法从 Azure Functions 运行此代码。我的问题是 - 是否有任何使用 .NET Core 2.1 发送 HTTP/2 请求的跨平台方式,或者 Azure Functions 2.0 是否有任何变通方法使其正常工作?

标签: c#.net-coreazure-functionsdotnet-httpclienthttp2

解决方案


我在 Azure Functions 2.0 上使用 WinHttpHandler 时遇到问题。我已经设法使用此处的解决方法解决了它。基本上,我将 .NET Standard 2.0 System.Net.Http.WinHttpHandler.dll 程序集放到函数项目的根目录中,并添加了以下内容:

  <ItemGroup>
    <None Update="System.Net.Http.WinHttpHandler.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  <Target Name="CopySPSM" BeforeTargets="Build">
    <Copy SourceFiles="System.Net.Http.WinHttpHandler.dll" DestinationFolder="$(OutputPath)\bin" />
  </Target>

有了它并设置了 WEBSITE_LOAD_USER_PROFILE = 1 来CngKey.Import为 APN 签名 JWT 令牌,它就开始工作了。


推荐阅读