今回はほぼリンクのブックマーク記事です。
dotnetcore 3.0 の時の新機能として HTTP/2 のサポートがありました。
リンク先に書かれていますが、ただ HttpClient を使うだけではなく少し設定? がいるとのことです。
設定と言ってもプロパティに使いたいバージョン ( 2.0 )を指定するだけです。
サンプルコードも掲載されていて一目瞭然です。
■ Request のプロパティに設定する場合
8 行目で HttpRequestMessage の Version プロパティにバージョン ( new Version(2, 0 ) )を設定しています。
var client = new HttpClient() { BaseAddress = new Uri("https://localhost:5001") }; // HTTP/1.1 request using (var response = await client.GetAsync("/")) Console.WriteLine(response.Content); // HTTP/2 request using (var request = new HttpRequestMessage(HttpMethod.Get, "/") { Version = new Version(2, 0) }) using (var response = await client.SendAsync(request)) Console.WriteLine(response.Content);
https://docs.microsoft.com/ja-jp/dotnet/core/whats-new/dotnet-core-3-0#http2-support より
■ HttpClient のプロパティに設定する場合
4 行目で HttpRequestMessage の DefaultRequestVersion プロパティにバージョン ( new Version(2, 0 ) ) を設定しています。
var client = new HttpClient() { BaseAddress = new Uri("https://localhost:5001"), DefaultRequestVersion = new Version(2, 0) }; // HTTP/2 is default using (var response = await client.GetAsync("/")) Console.WriteLine(response.Content);
https://docs.microsoft.com/ja-jp/dotnet/core/whats-new/dotnet-core-3-0#http2-support より
とても簡単ですね。(でも、またすぐに忘れそう)