How to reset settings in c# httpClient?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to reset settings in c# httpClient?



In c#, I make get and post requests. This is my code



GET


private async Task<string> GetAsync(string uri, Token token, string accept, string content_type)
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept)); // ACCEPT header
bool added = client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "text/xml");
if (token != null) client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.token_type, token.access_token);

HttpResponseMessage g = await client.GetAsync(uri);
if (g.IsSuccessStatusCode)

return await g.Content.ReadAsStringAsync();

else

errors.AddError(g.ReasonPhrase, await g.Content.ReadAsStringAsync());
return null;

}



POST


private async Task<string> PostAsync(string uri, Token token, string postData, string accept, string content_type)
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept)); // ACCEPT header
if (token != null) client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.token_type, token.access_token);

var content = new StringContent(postData, Encoding.UTF8, content_type);

HttpResponseMessage g = await client.PostAsync(uri, content);
if (g.IsSuccessStatusCode)

return await g.Content.ReadAsStringAsync();

else

errors.AddError(g.ReasonPhrase, await g.Content.ReadAsStringAsync());
return null;

}



But I read that you should reuse the httpclient like this


private static HttpClient client = new HttpClient();



as I make lots of frequent requests. However if I re-use the object, the settings like headers persist and that causes issues. Is there a way I can just reset the settings but keep the object?



Thanks





You can add the headers to the request object, instead of having them in the client's default request headers.
– maccettura
Aug 7 at 19:15






I think it's OK to instantiate a new instance if you need a blank slate. You just don't want to create a new one with every single request for no reason.
– John Wu
Aug 7 at 19:16





I don't think it will let you edit the attributes of the client once you've made a request with it.
– Marathon55
Aug 7 at 19:16






Unrelated, but why are you mixing async/await with synchronous code like client.GetAsync(uri).Result?
– Camilo Terevinto
Aug 7 at 19:19


async


await


client.GetAsync(uri).Result





If you're using asp.net core, you might take a look at using HttpClientFactory.
– Chris
Aug 7 at 19:21




1 Answer
1



Don't use the HttpClient's default headers. Set the headers on the request:


var content = new StringContent(postData, Encoding.UTF8, content_type) // CONTENT-TYPE header
content.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(accept)); // ACCEPT header
if (token != null)
content.Headers.Authorization = new AuthenticationHeaderValue(token.token_type, token.access_token);



Then all threads can use the same HttpClient throughout the runtime of the application without issue.





How would I do this, but still keep it async. I updated my code above. Can you edit your code to work with that?
– omega
Aug 7 at 19:59





@omega Headers is on the content object now after your edit.
– TheSoftwareJedi
Aug 7 at 20:46






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS