不是HttpClient发送的所有cookie? [英] Not all cookies sent by HttpClient?

查看:85
本文介绍了不是HttpClient发送的所有cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让HttpClient使用带有CookieContainer的HttpClientHandler来接收和发送cookie:

I'm trying to get HttpClient to receive and send cookies using an HttpClientHandler with a CookieContainer along the lines of:

CookieContainer cookieContainer = new CookieContainer();

HttpClientHandler = new HttpClientHandler()
{
    CookieContainer = cookieContainer
}

HttpClient client = new HttpClient(CookieHandler as HttpMessageHandler)
{
    BaseAddress = new Uri("https://secure.abc.xyz.com");
}

然后创建一个HttpRequestMessage以POST到登录资源"https://secure.abc.xyz.com/accounts/ api / login",并得到一个很好的响应,在我的cookie容器中设置两个cookie:

I then create a HttpRequestMessage to POST to a login resource "https://secure.abc.xyz.com/accounts/api/login", and get a good response that sets two cookies in my cookie container:


  • 一个安全的,httpOnly会话cookie"xyzSecure"具有空域和路径" / accounts"。
  • 非安全持久性cookie"xyzUser"域名".abc.xyz.com"和路径"/"。

通过查看以下调用的结果,我可以看到这些cookie被写入CookieContainer:

I can see that these cookies are written to the CookieContainer by looking at the results of the following calls:

CookieCollection cookies = CookieStorage.GetCookies(new Uri("https://abc.xyz.com"));
CookieCollection secureCookies = CookieStorage.GetCookies(new Uri("https://secure.abc.xyz.com/accounts"));

到目前为止,超好的。接下来,我向"https://secure.abc.xyz.com/accounts/api/orders/all""发送GET请求,请求失败,因为它是"xyzUser"。 cookie未包含在请求中。

So far, so good. Next, I send a GET request to "https://secure.abc.xyz.com/accounts/api/orders/all", and the request fails because it the "xyzUser" cookie wasn't included in the request.

当我使用任何浏览器(包括IE)和测试驱动程序页面通过Javascript执行相同的调用时,两个cookie都会被发送并且请求成功。

When I use any browser, including IE, with a test driver page that exercises these same calls via Javascript, both cookies are sent and the request succeeds.

可能是因为我错过了一些关于cookies工作方式的事情,但是不应该根据他们的域名和路径发送上述两个cookie吗? 我试过在HttpClient BaseAddress周围玩,复制"xyzUser"。来自"cookies"的cookie
到"secureCookies",以及其他几种查看我是否可以发送该cookie的方法,但无济于事。 关于我做错了什么想法?

Maybe it's the case that I'm missing something about the way cookies work, but shouldn't both cookies above be sent based on their domain and path?  I've tried playing around the the HttpClient BaseAddress, copying the "xyzUser" cookie from "cookies" to "secureCookies", and several other approaches to see if I can get that cookie sent, to no avail.  Any ideas on what I'm doing wrong?

谢谢!  John

Thanks!  John

推荐答案

看起来您正在使用代码中的不同变量。

It looks like you are using to different variables in your code.

cookieContainer应该是您的全局cookieContainer,您需要将其连接到您发出的任何请求。 我不确定CookieStorage来自哪里或是否在您的代码中创建。

cookieContainer should be your global cookieContainer and you would need to connect that to any request that you are issuing.  I am not sure where CookieStorage comes from or is created in your code.

如果您在公共云中有样本在更正代码后显示问题,那么发布它会很有用进一步调查。

If you have a sample in the public cloud that shows a problem after correcting your code, it would be useful to post for further investigation.

-Jeff


这篇关于不是HttpClient发送的所有cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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