Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a default request header for HttpClient

A bit related to my previous question I have the following:

public static HttpClient client= new HttpClient();
//Basic HTTP client setup
  client.BaseAddress = new Uri(address);
  client.DefaultRequestHeaders.Add("custom_header", "MyCustomHeader");

As you can see I set a base address (the matter of the previous question) that I can not change, and I set a custom header.

My question is can I change later in code this custom header (temporarily or permanently)?

For example I want my requests have the header "MyCustomHeader" but for some particular request, I want it to be "MyOtherHeader".

Is this possible, and if it is, how can I do it?

like image 466
KansaiRobot Avatar asked Jul 24 '26 10:07

KansaiRobot


1 Answers

As I understand, you want to add/remove this custom header on runtime.

You can add custom header like code below,

client.DefaultRequestHeaders.Add("custom_header", "MyCustomHeader");

And, you can remove header when you want with code below

client.DefaultRequestHeaders.Remove("custom_header");
like image 116
Onur Tekir Avatar answered Jul 27 '26 01:07

Onur Tekir