如何在Flurl HttpClient中保留标题 [英] How to preserve headers in the Flurl HttpClient

查看:165
本文介绍了如何在Flurl HttpClient中保留标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET Http客户端上使用Furl.Http包装器.对于每个请求,我的API都需要发送一个User-Agent和一个Authorization标头.

I'm using the Furl.Http wrapper over the .NET Http Client. With each request my API requires a User-Agent and an Authorization header to be sent.

我想设置一次,而不是每次都要声明.

I would like to set that up once, rather than having to state that every time.

我想我可以做的是创建一个FlurlClient实例,并在其上设置标头,然后在每个请求之前重设ResetToRoot,如以下示例代码所示:

What I thought I would be able to do is create an instance of the FlurlClient and set the headers on it, then ResetToRoot before each request, as illustrated in this sample piece of code:

    var fc = new FlurlClient();
    fc.WithHeader("User-Agent", "Keep/1.0");
    var tokenModel = await
        "https://app.tempuri.com".AppendPathSegment("auth")
        .WithClient(fc)
        .PostUrlEncodedAsync(new { username = "you", password = "secret"})
        .ReceiveJson<TokenModel>();

    fc.WithHeader("Authorization",
         string.Format("Token {0}",tokenModel.Token));                  

fc.Url.ResetToRoot();
    var userModel = await fc.Url
                          .AppendPathSegment("auth").GetJsonAsync<UserModel>();
    Console.WriteLine(userModel.Username);

但是,在RestToRoot()之后,似乎不再发送标头了.

However it would appear that after the RestToRoot() the headers are no longer sent.

这是设计使然吗?有没有更好的方法来确保在每个请求中发送这些标头?

Is this by design? Is there a better approach to insuring these headers are sent on each request?

推荐答案

问题出在倒数第二行.

fc.Url...GetJsonAsync

FlurlClient具有对Url对象的引用,反之亦然,因此,当您调用fc.Url时,实际上已经丢失了该引用,并且在幕后创建了一个新的FlurlClient当您呼叫GetJsonAsync时.这是设计使然,因为Flurl.Url是核心Flurl库中的简单生成器类,可以独立于Flurl.Http使用.

FlurlClient has a reference to the Url object, but not vice-versa, so by the time you call fc.Url, you've effectively lost that reference and a new FlurlClient is created behind the scenes when you call GetJsonAsync. This is by design in that Flurl.Url is a simple builder class in the core Flurl library that can be used independently of Flurl.Http.

这是我的处理方式:

var url = "https://app.tempuri.com";

using (var fc = new FlurlClient().WithHeader("User-Agent", "Keep/1.0")) {
    var tokenModel = await url
        .AppendPathSegment("...")
        .WithClient(fc)
        .PostUrlEncodedAsync(new { username = "you", password = "secret"})
        .ReceiveJson<TokenModel>();

    fc.WithHeader("Authorization",
        string.Format("Token {0}",tokenModel.Token));                  

    var userModel = await url
        .AppendPathSegment("...")
        .WithClient(fc)
        .GetJsonAsync<UserModel>();

    Console.WriteLine(userModel.Username);
}

一些注意事项:

  1. 请注意,您在每个HTTP调用中都附加了 FlurlClient.
  2. 这里url变量只是一个字符串,因此不需要调用ResetToRoot.
  3. using语句在这里不是绝对必需的,但这是一种好习惯.
  1. Notice you're attaching the FlurlClient in each HTTP call.
  2. Here the url variable is just a string, so there's no need to call ResetToRoot.
  3. The using statement is not absolutely required here but it is good practice.

这篇关于如何在Flurl HttpClient中保留标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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