9

我正在使用FlurlHttp并且我想为某些 API 调用禁用 AllowAutoRedirect。我知道如何让 System.Net.Http.HttpClient 不遵循 302 重定向?

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,我发现只有类似于 C# Flurl - Add WebRequestHandler to FlurlClient中描述的方式(我还没有编译下面的代码,所以它可能有一些错误)

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中查看更多选项

4

3 回答 3

6

自 Flurl 3.0 起,此答案已过时。根据公认的答案,Flurl 现在支持丰富的重定向功能。


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

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

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

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

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

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

然后像这样使用它:

new FlurlClient(url).WithoutRedirects()...
于 2017-10-07T01:25:00.157 回答
3

我的实现基于 Todd 的回答(次要类/方法名称更改)

使用示例:

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;
        }
    } 
于 2018-07-31T05:24:05.550 回答
3

Flurl 现在原生支持这个:

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

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

于 2021-05-21T18:10:04.017 回答