在FlurlClient中禁用自动重定向 [英] Disable AutoRedirect in FlurlClient

查看:232
本文介绍了在FlurlClient中禁用自动重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 FlurlHttp ,并且我想对某些API调用禁用AllowAutoRedirect. 我知道如何获取System.Net.Http.HttpClient不能跟随302重定向吗?

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

但是对于Flurl,我只发现了与 C#Flurl-将WebRequestHandler添加到FlurlClient (我尚未编译下面的代码,因此可能会有一些错误)

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

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

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?

推荐答案

感觉有点沉重,因为在这种情况下Flurl不直接支持,因此需要在后台进行一些修补.您走在正确的道路上,但我认为有几种方法可以简化它.首先,我建议在工厂内部创建WebRequestHandler.在外部创建它并传递它似乎是不必要的.

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.

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

如果您希望默认情况下在整个应用程序中使用此行为,则可以在启动时在全局范围内进行注册.然后,您无需对单个FlurlClient做任何事情.

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.

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

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

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:

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

然后像这样使用它:

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

这篇关于在FlurlClient中禁用自动重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆