Disable AutoRedirect in FlurlClient

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Disable AutoRedirect in FlurlClient



I am using FlurlHttp and I want to disable AllowAutoRedirect for some API calls.
I know How can I get System.Net.Http.HttpClient to not follow 302 redirects?


WebRequestHandler webRequestHandler = new WebRequestHandler();
webRequestHandler.AllowAutoRedirect = false;
HttpClient httpClient = new HttpClient(webRequestHandler);
// Send a request using GetAsync or PostAsync
Task<HttpResponseMessage> response = httpClient.GetAsync("http://www.google.com")



But for Flurl I found only the way similar to described in
C# Flurl - Add WebRequestHandler to FlurlClient (I haven't compiled yet the code below , so it may have some errors)


public class HttpClientFactoryWithWebRequestHandler : DefaultHttpClientFactory
{
private readonly WebRequestHandler _webRequestHandler;

public HttpClientFactoryWithWebRequestHandler (WebRequestHandler webRequestHandler )
{
_webRequestHandler = webRequestHandler ;
}

public override HttpMessageHandler CreateMessageHandler()
{
var handler =_webRequestHandler ;
//Or var handler = new WebRequestHandler(_webRequestHandler );
return handler;
}
}



Then I can pass the setting for a new FlurlClient:


WebRequestHandler webRequestHandler = new WebRequestHandler();
webRequestHandler.AllowAutoRedirect = false;
var fc = new FlurlClient(url)
.ConfigureClient(c => c.HttpClientFactory =
new HttpClientFactoryWithWebRequestHandler (webRequestHandler));



It looks more complicated that it could be. Is it the right way to do or it can be done simplier?





Is it ok to disable redirects on all clients app-wide?
– Todd Menier
Oct 7 '17 at 1:03







@ToddMenier, For current task it's OK, because the client is a microservice running as separate small MVC site
– Michael Freidgeim
Oct 7 '17 at 1:23






2 Answers
2



It feels a little heavy because it's a scenario that Flurl doesn't support directly, so it requires tinkering under the hood a bit. You're on the right track but I think there's a few ways you could simplify it. First, I'd suggest creating the WebRequestHandler inside the factory. Creating it externally and passing it in seems unnecessary.


WebRequestHandler


public class NoRedirectHttpClientFactory : DefaultHttpClientFactory
{
public override HttpMessageHandler CreateMessageHandler()
{
return new WebRequestHandler { AllowAutoRedirect = false };
}
}



If you want this behavior app-wide by default, you could register it globally on startup. Then you don't need to do anything with individual FlurlClients.


FlurlClient


FlurlHttp.Configure(settings =>
settings.HttpClientFactory = new NoRedirectHttpClientFactory());



Otherwise, if you need the ability to pick and choose which FlurlClients you disable it for, an extension method would make it a little easier:


FlurlClient


public static IFlurlClient WithoutRedirects(this IFlurlClient fc) {
fc.Settings.HttpClientFactory = new NoRedirectHttpClientFactory();
return fc;
}



Then use it like this:


new FlurlClient(url).WithoutRedirects()...





Thanks for your suggestion, but FlurlClient.HttpClient.AllowAutoRedirect = false; and client.AllowAutoRedirect = false; will not work. AllowAutoRedirect is a property of HttpClientHandler, not HttpClient. I wish to have FlurlClient.HttpClientHandler property.
– Michael Freidgeim
Oct 7 '17 at 1:53


FlurlClient.HttpClient.AllowAutoRedirect = false;


client.AllowAutoRedirect = false;


FlurlClient.HttpClientHandler





You are correct. Answer updated.
– Todd Menier
Oct 7 '17 at 14:28



My implementation is based on Todd's answer (minor class/method name changes):


public class HttpClientFactoryWithWebRequestHandler : DefaultHttpClientFactory
{
private readonly HttpMessageHandler _httpMessageHandler;
public HttpClientFactoryWithWebRequestHandler(HttpMessageHandler httpMessageHandler)
{
_httpMessageHandler = httpMessageHandler;
}

public override HttpMessageHandler CreateMessageHandler()
{
var handler = _httpMessageHandler;
return handler;
}
}
public static class FlurlClientExtensions
{
public static IFlurlClient DisableRedirects(this IFlurlClient fc)
{
var httpClientHandler = new HttpClientHandler {AllowAutoRedirect = false};
fc.Settings.HttpClientFactory = new HttpClientFactoryWithWebRequestHandler(httpClientHandler);
return fc;
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results