在FlurlClient中禁用AutoRedirect

时间:2017-10-07 00:30:54

标签: redirect dotnet-httpclient asp.net-core-webapi flurl

我正在使用FlurlHttp,我想为某些API调用禁用AllowAutoRedirect。 我知道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")

但对于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));

它可能看起来更复杂。这是正确的做法还是可以做得更简单?

3 个答案:

答案 0 :(得分:3)

感觉有点沉重,因为这是Flurl不直接支持的场景,因此需要稍微修改一下。您已走上正轨,但我认为您可以采用一些方法来简化它。首先,我建议在工厂内创建<svg>。在外部创建并传递它似乎是不必要的。

WebRequestHandler

如果您希望在应用程序范围内执行此行为,则可以在启动时全局注册。那么你不需要对个人public class NoRedirectHttpClientFactory : DefaultHttpClientFactory { public override HttpMessageHandler CreateMessageHandler() { return new WebRequestHandler { AllowAutoRedirect = false }; } } 做任何事情。

FlurlClient

否则,如果您需要能够选择禁用它的FlurlHttp.Configure(settings => settings.HttpClientFactory = new NoRedirectHttpClientFactory()); ,则扩展方法会使其更容易:

FlurlClient

然后像这样使用它:

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

答案 1 :(得分:2)

Flurl 现在原生支持:

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

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

答案 2 :(得分:1)

我的实现基于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;
        }
    }