You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework 4.8.1 WinForm在Win10与Win11的HTTP2请求异常问题

.NET Framework 4.8.1应用在Windows 10上请求CloudFlare返回403问题

我使用.NET Framework 4.8.1开发了一款应用,在开发所用的Windows 11电脑上运行正常,但在两台全新安装并更新的Windows 10电脑上出现异常。

一、背景信息

应用通过HTTP2协议向远程CloudFlare服务器发送HTTP请求以获取令牌。

发送给服务器的请求:

{Method: POST, RequestUri: 'https://this.is.confidential/json/gov/v10/Api/CreateApiKey', Version: 2.0, Content: System.Net.Http.StringContent, Headers:
{
  Content-Type: text/plain; charset=utf-8
}}

Windows 11电脑上收到的服务器响应:

{StatusCode: 200, ReasonPhrase: '', Version: 2.0, Content: System.Net.Http.NoWriteNoSeekStreamContent, Headers:
{
  Cache-Control: no-store, no-cache
  Date: Tue, 13 Dec 2022 19:37:43 GMT
  Server: cloudflare
  Set-Cookie: __cf_bm=W5wDtxsixycI1_5G5fdgdfJGkVCX5nT9tzG8-1670960263-0-AfqDRz1MMF67L/ntDOShg3Jz3GZxMo/UtCYReTfDIJl1g0vQ/MfeVa5C8/PxQLtucWoYvV5nYGEyyuN/r19aWWo=; path=/; expires=Tue, 13-Dec-22 20:07:43 GMT; domain=.thisisconfidential.com; HttpOnly; Secure; SameSite=None
  Vary: Accept-Encoding
  access-control-allow-origin: *
  x-duration: 33.200026
  cf-cache-status: DYNAMIC
  cf-ray: 779127ecadedf11c-CDG
  Content-Type: application/json
}}

Windows 10电脑上收到的服务器响应:

{StatusCode: 403, ReasonPhrase: '', Version: 2.0, Content: System.Net.Http.NoWriteNoSeekStreamContent, Headers:
{
  Cache-Control: no-store, must-revalidate, no-cache, max-age=0, private, post-check=0, pre-check=0
  Date: Tue, 13 Dec 2022 13:18:28 GMT
  Server: cloudflare
  Set-Cookie: __cf_bm=oKXtpXuiUmH5MeEA8n67RTG33dfZhv5Th5c3ZX0PAUSw1oA-1670937508-0-AX+aH9+MYxwNqOdbMZCmhg35ByjwgaTmtJdditJ7K1xEU1ex7au/PyiJ4JbCImQvICHQPcr+QJijZSeYfKme3/o=; path=/; expires=Tue, 13-Dec-22 13:48:28 GMT; domain=.thisisconfidential.com; HttpOnly; Secure; SameSite=None
  x-frame-options: SAMEORIGIN
  referrer-policy: same-origin
  cf-ray: 778efc668ed6d07b-CDG
  Content-Length: 16
  Content-Type: text/plain; charset=UTF-8
  Expires: Thu, 01 Jan 1970 00:00:01 GMT
}}

二、代码实现

为在.NET 4.8.1中使用HTTP2,自定义了一个HttpHandler类:

Imports System.Net.Http

Public Class Http2CustomHandler : Inherits WinHttpHandler
    Protected Overrides Function SendAsync(request As HttpRequestMessage, cancellationToken As Threading.CancellationToken) As Task(Of HttpResponseMessage)
        request.Version = New Version("2.0")
        Return MyBase.SendAsync(request, cancellationToken)
    End Function
End Class

发送请求时调用该类的代码:

Using wclient As New HttpClient(New Http2CustomHandler())
    Dim url As String = "https://this.is.confidential.com/json/gov/v10/Api/CreateApiKey"
    With wclient
        .BaseAddress = New Uri(url)
        .DefaultRequestHeaders.UserAgent.Add(New Headers.ProductInfoHeaderValue("govAgent", "3.6.15"))
        .DefaultRequestHeaders.Accept.Clear()
        .DefaultRequestHeaders.Accept.Add(New Headers.MediaTypeWithQualityHeaderValue("text/plain"))
    End With

    Dim req = New HttpRequestMessage With {
        .RequestUri = New Uri("https://this.is.confidential.com/json/gov/v10/Api/CreateApiKey"),
        .Method = HttpMethod.Post,
        .Content = New StringContent($"login={login}&password={password}&gov={gov}&long_life_token=true", Encoding.UTF8, "text/plain")
    }

    Dim response As HttpResponseMessage = Await wclient.SendAsync(req)
    Dim jsonString = response.Content.ReadAsStringAsync().Result

三、测试信息

  • 将包含所有必要DLL的Release文件夹复制到Windows 10电脑上运行应用,出现问题
  • 尝试发布并安装应用,同样出现问题
  • 将VB.NET项目移至Windows 10电脑编译运行,请求仍失败
  • 两台Windows 10和开发用的Windows 11均已更新至最新版本,且都安装了.NET Framework 4.8.1

更新

实际是Windows 10与.NET Framework 4.8/4.8.1结合WinHttpHandler存在BUG,使用HTTP 2.0版本时无法正常工作,但改用HTTP 3.0版本即可兼容CloudFlare WAF。


内容的提问来源于stack exchange,提问作者SuperJudeFruit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 23:20:27