// Create a new HttpClient instance HttpClient client = new HttpClient(); // Add an Authorization header with a bearer token client.DefaultRequestHeaders.Add("Authorization", "Bearer myToken123");
// Create a new HttpClient instance HttpClient client = new HttpClient(); // Add a custom User-Agent header client.DefaultRequestHeaders.UserAgent.TryParseAdd("MyApp/1.0");This code snippet shows how to set a custom User-Agent header by using the HttpHeaders.UserAgent property. The TryParseAdd method lets the caller add a new value to the collection without overwriting the existing header. Both examples use the same HttpClient class and the System.Net.Http namespace. The AddRequestHeader method is part of the DefaultRequestHeaders property of the HttpClient class. Therefore, the package library required is System.Net.Http.