2

I thought .net 4.8 supports http/2, but seems I am wrong. In my test program:

var requestMessage= new HttpRequestMessage(HttpMethod.Post, uri)
{
                Content = new StringContent(body, Encoding.UTF8, "application/json"),
                Headers = { Authorization = new AuthenticationHeaderValue("Bearer", token) },
                Version = new System.Version(2,0)
};

var response = _httpClient.SendAsync(requestMessage).Result;

I got error: Only HTTP/1.0 and HTTP/1.1 version requests are currently supported.

Does it mean I still need to use the custom WinHttpHandler in .net 4.8?

daxu
  • 3,514
  • 5
  • 38
  • 76
  • Is this a client or server error? Post the *full* exception text, not just the message. You can get the full text with `Exception.ToString()` or by clicking on `Copy Details` in Visual Studio's exception popup. You can't for a server to use HTTP/2 if it doesn't support it – Panagiotis Kanavos Feb 10 '22 at 14:28
  • HTTP/2 requires HTTPS too. What's the actual URL? PS: `.Result` is a bad idea. Make the methods asynchronous instead of blocking the asynchronous methods – Panagiotis Kanavos Feb 10 '22 at 14:29
  • You can install [a NuGet package](https://www.nuget.org/packages/System.Net.Http.WinHttpHandler/6.0.0) with a custom `HttpHandler` to add http/2 support - see [this answer](https://stackoverflow.com/a/43101990/124386) for details. – Richard Deeming Feb 10 '22 at 14:42
  • yes, it is just some bad test code I created to see what is going on – daxu Feb 10 '22 at 14:42
  • 1
    ok, I found this doc clearly says how http 2 is supported in .net 4.8. So looks like I will need to use custom HttpHandler https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8 – daxu Feb 10 '22 at 14:52

0 Answers0