KansaiRobot
KansaiRobot

Reputation: 9912

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?

Upvotes: 7

Views: 10592

Answers (1)

Onur Tekir
Onur Tekir

Reputation: 250

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");

Upvotes: 11

Related Questions