WinRT's HttpClient
lacks the .NET Credential
class for setting authorization headers. This article details a workaround for adding authorization headers, specifically using OAuth tokens.
The Challenge: How to add an authorization header (e.g., for OAuth) to a WinRT HttpClient
request?
The Solution: Utilize the AuthenticationHeaderValue
class:
<code class="language-csharp">httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Your OAuth token");</code>
This code snippet constructs an authentication header using the "Bearer" scheme and your OAuth token. Adding this to DefaultRequestHeaders.Authorization
ensures the header is automatically included in all subsequent requests made by this HttpClient
instance.
The above is the detailed content of How to Set Authorization Headers in WinRT's HttpClient?. For more information, please follow other related articles on the PHP Chinese website!