I read a lot about best practices when using HttpClient
. Most people recommend reusing it throughout the life of the application, even if it is IDisposable
.
My web application interacts with various APIs such as Facebook API, Twitter API and Instagram API.
The plan is to create a separate HttpClient for each API that it communicates with, which is recommended, because then I can reuse some of the headers.
But now to the question, let's take the Twitter API as an example, each user using my web application has its own authorization header (user access token). I believe this means that I cannot set the authorization header for the DefaultRequestHeaders of the HttpClient object.
What is the best practice of reusing HttpClient
for multiple users who have different privilege headers?
Can I create an HttpRequestMessage
object for each request and set the authorization header in the httpRequestMessage object instead of setting it to the default, httpClient.DefaultRequestHeaders.Authorization
?
Thanks.
source share