using System.Net.Http; HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://www.example.com"); request.Headers.Add("Authorization", "Bearer myToken");
using System.Net.Http; HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://www.example.com"); request.Headers.Add("X-Custom-Header", "CustomValue");
using System.Collections.Generic; using System.Net.Http; HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://www.example.com"); DictionaryIn this example, multiple headers are added to the request using a dictionary to store the header key-value pairs. A foreach loop is used to iterate over the dictionary and add each header to the request. Package library: System.Net.Http and System.Collections.Genericheaders = new Dictionary () { { "Content-Type", "application/json" }, { "Authorization", "Bearer myToken" }, }; foreach (KeyValuePair header in headers) { request.Headers.Add(header.Key, header.Value); }