我如何从HttpClientHandler获得cookie.在使用Web API自托管时使用CookieContaine
本文关键字:API CookieContaine Web HttpClientHandler 获得 cookie | 更新日期: 2023-09-27 18:04:43
我正在尝试为返回cookie的Web API方法编写集成测试。基于这个问题,我认为我需要设置一个HttpClientHandler的CookieContainer。
HttpClient不保存Cookies
然后,基于这个问题,我认为我需要将这个HttpClientHandler设置为我的HttpServer实例的InnerHandler,以便我可以链接处理程序。
如何执行HttpClientHandler的内存测试
问题是我仍然发现CookieContainer是空的。我有一个Assert来验证响应中是否有一个包含我所期望的cookie的报头。下面是我的代码:
// Set up in-memory hosting of Web API
var config = new HttpConfiguration();
WebApiConfig.Register(config);
var cookieJar = new CookieContainer();
var httpClientHandler = new HttpClientHandler
{
CookieContainer = cookieJar,
UseCookies = true
};
var httpServer = new HttpServer(config)
{
InnerHandler = httpClientHandler
};
const string loginUrl = "http://localhost/api/authentication/login";
var uri = new Uri(loginUrl);
var client = new HttpClient(httpServer) { BaseAddress = uri };
var response =
client.PostAsJsonAsync(uri,
new LoginMessage {Email = UserEmail, Password = Password},
CancellationToken.None).Result;
response.EnsureSuccessStatusCode();
var data = response.Content.ReadAsAsync<LoginResponse>().Result;
Assert.IsTrue(response.Headers.Contains("Set-Cookie"));
Assert.AreNotEqual(0, httpClientHandler.CookieContainer.Count);
对CookieContainer中项目数量的最后一个Assert失败。我还尝试调用httpClientHandler.CookieContainer.GetCookies(uri)
,但结果cookie集合仍然包含零元素。
我也试过显式地链接我的客户端处理程序,但是没有看到行为上的区别。
var cookieJar = new CookieContainer();
var httpClientHandler = new HttpClientHandler
{
CookieContainer = cookieJar,
UseCookies = true
};
var httpServer = new HttpServer(config);
var client = HttpClientFactory.Create(httpClientHandler, httpServer);
谁能告诉我我在试图检索这些cookie时做错了什么?我在响应头中看到的cookie是这样的:
Temp=E4C4947BAB708D089C636B4A5BDA094A10C8B1D66347EBD96629CB9BDB589822198A09BCBBE22C13D07AFC4D4C3C91E78C08BECFC9CC03BEE881858A1B4752EDC1BB1E604B819FBAFE34776B6F46B78ADCD2421F8125A395784981BFF22CEBE19DB228158E85B5786279E55D4114B72E50098B2374C23A43B7C89EBA30F771C54E1D98A6B1CD315D1FB8623E8E97E41FE4279479B49D0A948E2752CA09BD17FF7B52764E96EA43DB9E98D7CEBD8A73C9547A696C170CFC9B6F75CA23B881ED9675C2344A2A16BEF2DFD70DBA723FB62578E7779ECADCF11347B22199DAC8FA6A; expires=Thu, 22 May 2014 01:37:36 GMT; path=/
根据我从Darrel Miller收到的关于原始问题的评论,似乎处理这个问题的唯一方法是如果我们有一个使用HttpServer作为内部处理程序的委托处理程序。
我最终构建了我自己的CookieDelegateHandler,看起来像这样:
public class CookieDelegateHandler : DelegatingHandler
{
public CookieContainer CookieContainer { get; set; }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return
base.SendAsync(request, cancellationToken).ContinueWith(responseToCompleteTask
=>
{
var result = responseToCompleteTask.Result;
var cookieHeaders = result.Headers.Where(pair => pair.Key == "Set-Cookie");
foreach (var value in cookieHeaders.SelectMany(header => header.Value))
{
CookieContainer.SetCookies(request.RequestUri, value);
}
return result;
}, TaskContinuationOptions.OnlyOnRanToCompletion);
}
}
通过这个实现,我可以像这样实例化HttpClient:
var client = new HttpClient(new CookieDelegateHandler
{
CookieContainer = new CookieContainer(),
InnerHandler = new HttpServer(config)
});
执行委托处理程序的延续,并将cookie添加到CookieContainer中。
我欢迎任何关于如何在没有这个自定义委托处理程序的情况下解决这个问题的其他想法,或者关于我的解决方案中是否存在问题的评论。