Dela via


HttpClient.DefaultRequestHeaders Property

Definition

Gets a collection of headers that should be sent with each request. For programming guidance for the HttpClient class, and code examples, see the HttpClient conceptual topic.

public:
 property HttpRequestHeaderCollection ^ DefaultRequestHeaders { HttpRequestHeaderCollection ^ get(); };
HttpRequestHeaderCollection DefaultRequestHeaders();
public HttpRequestHeaderCollection DefaultRequestHeaders { get; }
var httpRequestHeaderCollection = httpClient.defaultRequestHeaders;
Public ReadOnly Property DefaultRequestHeaders As HttpRequestHeaderCollection

Property Value

The headers that should be sent with each request.

Remarks

The DefaultRequestHeaders property returns an HttpRequestHeaderCollection object that can be used to get or set the specific headers on the HttpClient instance. HTTP headers set on this property will be sent on all request messages sent on this HttpClient instance and don't need to be set on each HttpRequestMessage instance.

The Headers property on the HttpRequestMessage object returns an HttpRequestHeaderCollection object that can be used to get or set the specific headers on a specific HTTP request.

The DefaultRequestHeaders property represents the headers that an app developer can set, not all of the headers that may eventually be sent with the request. The HttpBaseProtocolFilter will add some additional headers.

Warning

In versions before Windows 10, setting certain headers to NULL caused an empty header value to be set, which caused an unexpected failure in subsequent calls to GetAsync. Starting in Windows 10, setting any of the following headers to NULL causes them to be removed from the request entirely, so that the remaining headers are valid.

  • Authorization
  • Date
  • From
  • Host
  • If-Modified-Since
  • If-Unmodified-Since
  • Max-Forwards
  • Proxy-Authorization
  • Referer

If you are writing for versions before Windows 10, do not set these header values to NULL.

Applies to

See also