【问题标题】:HttpRequest Http 2.0HttpRequest Http 2.0
【发布时间】:2022-02-21 20:15:02
【问题描述】:

我正在尝试发出 HTTP/2.0 请求,但无法正常工作。根据这个和其他帖子https://stackoverflow.com/a/53776383/18171693.NET > 3.0 应该支持它,但我无法让它工作。

       Debug.WriteLine("Version:"+ Environment.Version.ToString());

        var client = new HttpClient();

        var req = new HttpRequestMessage(HttpMethod.Get,"https://http2.akamai.com/demo")
        {
            Version = new Version(2, 0)
        };

        var x = await client.SendAsync(req);

上面的代码生成:

Version:4.0.30319.42000
Exception thrown: 'System.ArgumentException' in mscorlib.dll
An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code
Only HTTP/1.0 and HTTP/1.1 version requests are currently supported.

【问题讨论】:

    标签: c# http2 httpconnection


    【解决方案1】:

    您没有使用 .NET-Core。

    您使用的版本 - 4.0.30319.42000 - 对应于 .NET Framework 4.6+,而不是 .NET 核心。

    来自How to: Determine which .NET Framework versions are installed

    公共语言运行时 (CLR),它管理和执行您的 应用程序的代码。单个 CLR 版本通常支持多个 .NET 框架版本。例如,CLR 版本 4.0.30319.xxxxx 其中 xxxxx 小于 42000,支持 .NET Framework 版本 4 到 4.5.2. 大于或等于 4.0.30319.42000 的 CLR 版本支持从 .NET Framework 4.6 开始的 .NET Framework 版本

    .NET Framework 仅支持 Http 版本 1/1.1,但是这个问题 How to make the .net HttpClient use http 2.0? 有一些解决方法。

    我已经测试了你的代码,它适用于 .NET core 3.1 和 .NET 6。Demo in .NET 6

    【讨论】:

    • 非常感谢!我安装了 .NET 6,然后它运行良好。乍一看,我在 Windows 窗体中找不到图表控件,但我想这是另一个问题 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多