2017-09-24 22 views
2

Je possède ce gestionnaire de requêtes:Comment ajouter HttpClientHandler à url?

var httpClientHandler = new HttpClientHandler 
{ 
    Proxy = new WebProxy(proxy.Address, proxy.Port), 
    UseProxy = true 
}; 

Et:

var url = new Url(hostUrl) 
    .AppendPathSegment(pathSegment); 

Comment ajouter le gestionnaire de requêtes au FlurlClient?

Répondre

2

Créez votre propre ProxiedHttpClientFactory qui remplace la méthode CreateMessageHandler():

public class ProxiedHttpClientFactory : DefaultHttpClientFactory 
{ 
    private readonly string _proxyAddress; 
    private readonly int _proxyPort; 

    public ProxiedHttpClientFactory(string proxyAddress, int proxyPort) 
    { 
     this._proxyAddress = proxyAddress; 
     this._proxyPort = proxyPort; 
    } 

    public override HttpMessageHandler CreateMessageHandler() 
    { 
     return new HttpClientHandler 
     { 
      Proxy = new WebProxy(this._proxyAddress, this._proxyPort), 
      UseProxy = true 
     }; 
    } 
} 

, utilisez-le:

var settings = new FlurlHttpSettings 
{ 
    HttpClientFactory = new ProxiedHttpClientFactory("my.proxy.com", 8080) 
}; 

var client = new FlurlClient(settings); 

Et sur une instance existante Url:

var url = new Url(hostUrl) 
       .AppendPathSegment(pathSegment) 
       .ConfigureClient(settings => settings.HttpClientFactory = new ProxiedHttpClientFactory("my.proxy.com", 8080));