1

爲什麼添加DelegatingHandler後無法添加驗證細節?向httpclient添加處理程序後無法添加構造函數

之前加入DelegatingHandler,我能夠實例化HttpClient像這樣:

var client = new HttpClient{Credentials = ..., BaseAddress = ...}; 

已經增加了DelegatingHandler後,我無法添加CredentialsBaseAddress

var client = new HttpClient(new RetryHandler(new HttpClientHandler())); 
client.BaseAddress = // does not exist! 
client.Credentials = // does not exist! 

我們如何向這個客戶端添加憑證和基礎地址?

回答

2

這是你可以憑據傳遞到您的處理程序:

var clientHandler = new HttpClientHandler() 
{ 
    Credentials = ... 
}; 

var retryHandler = new RetryHandler(clientHandler); 

var client = new HttpClient(retryHandler) 
{ 
    BaseAddress = ... 
}; 
相關問題