Home > Net >  HttpRequest Http 2.0
HttpRequest Http 2.0

Time:02-20

I am trying to make a HTTP/2.0 request but cant get it to work. According to this and other posts https://stackoverflow.com/a/53776383/18171693 .NET > 3.0 shall support it but I can not get it to work.

       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);

The code above generates:

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.

CodePudding user response:

You're not using .NET-Core.

The version you're using - 4.0.30319.42000 - corresponds to .NET Framework 4.6 , not .NET core.

From How to: Determine which .NET Framework versions are installed:

The common language runtime (CLR), which manages and executes your app's code. A single CLR version typically supports multiple .NET Framework versions. For example, CLR version 4.0.30319.xxxxx where xxxxx is less than 42000, supports .NET Framework versions 4 through 4.5.2. CLR version greater than or equal to 4.0.30319.42000 supports .NET Framework versions starting with .NET Framework 4.6.

Only Http versions 1/1.1 are supported in .NET Framework, however this question How to make the .net HttpClient use http 2.0? has some workarounds.

I've tested your code, which works for me on .NET core 3.1 and .NET 6. Demo in .NET 6

  • Related