30
13

More than 1 year has passed since last update.

HttpClient でリクエストヘッダを設定する (C#)

Last updated at Posted at 2018-08-04

HttpClient でリクエストヘッダを設定する (C#)

備忘録として.
また、ググったらHttpClient.DefaultRequestHeaders.Addだらけでイラッとしたので.

ざっくり言えばHttpRequestMessageを作りHttpRequestMessage.Headers.Addして、HttpClient.SendAsyncに渡す.

以下コード例.

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("Content-Type", "application/json");
request.Headers.Add("Authorization", $"Bearer {accessToken}");
var response = await client.SendAsync(request);
30
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
13