从Flurl.Http v2.0.1中的请求取回cookie [英] Getting back cookies from the request in Flurl.Http v2.0.1
问题描述
使用Flurl.Http v1.2时,我们具有以下代码:
When using Flurl.Http v1.2 we had the following code:
1. var cookieJar = new CookieContainer();
2. var url = baseUrl.AppendPathSegment("api/auth/login");
3. var client = url.EnableCookies();
4. ((HttpClientHandler)client.HttpMessageHandler).CookieContainer = cookieJar;
5. var result = await client.PostJsonAsync(new { UserName = userName, Password = password });
6. var cookies = cookieJar.GetCookies(new Uri(baseUrl));
7. _cookie = cookies[0];
根据发行说明,升级到v2.0.1后,第4行不再编译,因为client
不再是IFlurlClient
,而现在是IFlurlRequest
.
After upgrading to v2.0.1 line 4 no longer compiles because client
is no longer an IFlurlClient
it's now an IFlurlRequest
, as per the release notes.
我注意到IFlurlRequest
具有Client
属性,因此我将第4行更改为:
I noticed that IFlurlRequest
has a Client
property, so I changed line 4 to be:
4. ((HttpClientHandler)client.Client.HttpMessageHandler).CookieContainer = cookieJar;
现在可以编译,但在运行时失败,并显示InvalidOperationException:
That now compiles but fails at run-time with an InvalidOperationException:
此实例已启动一个或多个请求.只能在发送第一个请求之前修改属性.
This instance has already started one or more requests. Properties can only be modified before sending the first request.
我假设这是由于底层HttpClient的主动重用.我在3到4之间添加了一行,以每次创建一个新的FlurlClient来确保实例无法启动请求.
I'm assuming that's because of the aggressive reuse of the underlying HttpClient. I added a line between 3 and 4 to create a new FlurlClient each time to ensure the instance could not have started a request.
1. var cookieJar = new CookieContainer();
2. var url = baseUrl.AppendPathSegment("api/auth/login");
3. var request = url.EnableCookies();
3.5 request.Client = new FlurlClient();
4. ((HttpClientHandler)request.Client.HttpMessageHandler).CookieContainer = cookieJar;
5. var result = client.PostJsonAsync(new { UserName = userName, Password = password }).Result;
6. var cookies = cookieJar.GetCookies(new Uri(baseUrl));
7. _cookie = cookies[0];
现在可以再次使用,但是我不确定我是否以正确的方式进行操作.任何反馈将不胜感激.
This now works again, but I'm not sure I'm doing this the correct way. Any feedback would be appreciated.
推荐答案
您不需要显式管理CookieContainer
; Flurl使使用Cookie的工作变得简单得多.
You shouldn't need to manage CookieContainer
explicitly; Flurl makes working with cookies a lot simpler.
通常,您希望尽可能多地重用HttpClient
实例,默认情况下,当您不显式创建FlurlClient
时,2.0会为您执行此操作,但是如果在多个调用中保留cookie,可能要自己管理FlurlClient
,否则多个线程可能正在同一集合上读取/写入cookie.
Generally you want to reuse HttpClient
instances as much as possible, and 2.0 does do this for you by default when you don't create FlurlClient
s explicitly, but in the case of persisting cookies over multiple calls you're probably going to want to manage FlurlClient
yourself, otherwise multiple threads could be reading/writing cookies on the same collection.
仍然很容易.我认为您的整个示例可以简化为:
Still pretty easy. I think your whole example can be reduced to this:
using (var cli = new FlurlClient(baseUrl).EnableCookies()) {
await cli.Request("api/auth/login").PostJsonAsync(new {
UserName = userName,
Password = password });
_cookie = cli.Cookies.First().Value;
}
这篇关于从Flurl.Http v2.0.1中的请求取回cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!