Julian Bechtold Julian Bechtold -3 years ago 137
C# Question

How to add a header to a single http request rather than to all as default

I have to add a header value for a single request only. all I´ve found was

client.DefaultRequestHeaders.Add("X-Version","1");


As far as I understood, this would affect all future requests. But I want it for one single request only.

Is there a possibility to do that? I dont want to create a new httpclient just for one request since I have to pass session cookies and so on.

In this particular case, I want to add a referrer.

Best regards and thanks for help

Answer Source

Thats my solution which I came up with

client.DefaultRequestHeaders.Add("Referrer",GlobalVar.baseURI + referrer) ;
string responseString = client.GetStringAsync(GlobalVar.baseURI +loan + "/investment").Result;
client.DefaultRequestHeaders.Remove("Referrer");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download