5

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

1 Answers1

8

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");
Onur Tekir
  • 220
  • 1
  • 3