我正在尝试为返回Cookie的Web API方法编写集成测试。基于这个问题,我相信我需要设置一个HttpClientHandler的CookieContainer。

HttpClient Not Saving Cookies

然后,基于这个问题,我认为我需要将此HttpClientHandler设置为HttpServer实例的InnerHandler,以便可以链接处理程序。

How to perform in-memory test of HttpClientHandler

问题是我仍然发现CookieContainer为空。我有一个断言,可以验证响应中是否包含标题,该标题包含了我所期望的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中项目数的最后一次声明失败。我也尝试调用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。

如果没有此自定义委派处理程序,或者在我的解决方案中是否存在问题的评论,我将欢迎提出任何其他关于如何解决此问题的想法。

关于c# - 使用Web API自托管时,如何从HttpClientHandler.CookieContainer获取cookie?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23797158/

10-13 01:47