在 FlurlClient 中禁用自动重定向

Disable AutoRedirect in FlurlClient

我正在使用 FlurlHttp,我想为某些 API 调用禁用 AllowAutoRedirect。 我知道

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")

但是对于Flurl,我只找到了类似于 (下面的代码我还没有编译,所以可能有一些错误)

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;
    }
}

然后我可以传递新 FlurlClient 的设置:

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

看起来比实际情况更复杂。这是正确的做法还是可以更简单地完成?

2021 年更新:Flurl 现在支持开箱即用

await url.WithAutoRedirect(false).GetAsync();

https://flurl.dev/docs/configuration/#redirects

中查看更多选项

自 Flurl 3.0 起,此答案已过时。根据已接受的答案,Flurl 现在 supports 开箱即用的大量重定向功能。


感觉有点沉重,因为它是 Flurl 不直接支持的场景,因此需要在引擎盖下进行一些修补。你走在正确的轨道上,但我认为有几种方法可以简化它。首先,我建议在工厂内创建 WebRequestHandler 。在外部创建它并传入它似乎是不必要的。

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

如果您希望默认在整个应用程序范围内执行此行为,您可以在启动时全局注册它。那么你不需要对个人 FlurlClients 做任何事情。

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

否则,如果您需要能够挑选并选择要禁用它的 FlurlClient,扩展方法会使它更容易一些:

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

然后像这样使用它:

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

我的实现是基于托德的回答(class/method 名称的小改动)

使用示例:

var resp = await new FlurlClient(url).DisableRedirects().Request().AllowAnyHttpStatus().GetAsync();
resp.StatusCode.Should().Be(HttpStatusCode.Found);//302

实施:

 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;
        }
    } 

Flurl 现在原生支持:

"https://example.com"
  .WithAutoRedirect(false)
  [...]

文档:https://flurl.dev/docs/configuration/#redirects