Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass HttpClientHandler to existing HttpClient?

I have HttpClient httpClient injected from outside of my class.

Inside my class I'm creating a new HttpClientHandler var handler = new HttpClientHandler() and setting its values.

I need to link HttpClientHandler to existing HttpClient the same way you can do it using the constructor: new HttpClient(handler)

Is it possible? I've tried looking for HttpClientHandler property within HttpClient but it does not exist. Is there any other way?

like image 200
Mariusz Ignatowicz Avatar asked Apr 11 '26 05:04

Mariusz Ignatowicz


1 Answers

No, you can't.

  • The HttpClient class does not hold a reference to the underlying HttpMessageHandler.
  • Whenever you pass either an HttpClientHandler or a SocketsHttpHandler to the HttpClient's constructor then it is passing the handler to its base class's constructor.
  • Its base class is a HttpMessageInvoker where the _handler field is defined as readonly.
  • So, you can't change it after you have created an HttpClient instance.
like image 83
Peter Csala Avatar answered Apr 13 '26 21:04

Peter Csala