将 cookie 从第一个响应复制到下一个请求
我使用 HttpRequest->send() 发送第一个 HTTP 请求,并收到带有以下 Set-Cookie 标头的 302 响应:
- Set-Cookie: SESSION_SCOPE=1;路径=/
- 设置Cookie: III_EXPT_FILE=aa2171;路径=/;域=.example.com
- 设置 Cookie:III_SESSION_ID=193a3ce5aaadea85937c25cd0430332f;域名=.example.com; path=/
当我使用 HttpRequest->getResponseCookies() 时,提取的内容如下所示:
Array (
- [0] => stdClass Object ( [cookies] => Array ( [SESSION_SCOPE] => 1 ) [extras] => Array ( ) [flags] => 0 [expires] => 0 [path] => / [domain] => )
- [1] => stdClass Object ( [cookies] => Array ( [III_EXPT_FILE] => aa2171 ) [extras] => Array ( ) [flags] => 0 [expires] => 0 [path] => / [domain] => .example.com )
- [2] => stdClass Object ( [cookies] => Array ( [III_SESSION_ID] => 193a3ce5aaadea85937c25cd0430332f ) [extras] => Array ( ) [flags] => 0 [expires] => 0 [path] => / [domain] => .example.com )
)
现在我需要将这些 cookie 复制到重定向位置的下一个传出请求。我正在使用 HttpRequest->setCookies(),其中参数是从先前的 getResponseCookies() 调用返回的数组。
我在传出的请求中看到的是:
Cookie: 0%5Bcookies%5D%5BSESSION_SCOPE%5D=1; 0%5Bflags%5D=0; 0%5Bexpires%5D=0; 0%5Bpath%5D=%2F; 0%5Bdomain%5D=; 1%5Bcookies%5D%5BIII_EXPT_FILE%5D=aa2171; 1%5Bflags%5D=0; 1%5Bexpires%5D=0; 1%5Bpath%5D=%2F; 1%5Bdomain%5D=.example.com; 2%5Bcookies%5D%5BIII_SESSION_ID%5D=193a3ce5aaadea85937c25cd0430332f; 2%5Bflags%5D=0; 2%5Bexpires%5D=0; 2%5Bpath%5D=%2F; 2%5Bdomain%5D=.example.com
我的问题是:
- 这样做的正确方法是什么?因为显然数组索引也被添加到标头中,
- 如何防止参数的 url 编码?
- 如何防止将“路径”和“域”属性添加到标头?
谢谢!
I'm sending a first HTTP request out using HttpRequest->send(), and I receive a 302 response with the following Set-Cookie headers:
- Set-Cookie: SESSION_SCOPE=1; path=/
- Set-Cookie: III_EXPT_FILE=aa2171; path=/; domain=.example.com
- Set-Cookie: III_SESSION_ID=193a3ce5aaadea85937c25cd0430332f; domain=.example.com; path=/
When I use HttpRequest->getResponseCookies(), this is what the extracted content looks like:
Array (
- [0] => stdClass Object ( [cookies] => Array ( [SESSION_SCOPE] => 1 ) [extras] => Array ( ) [flags] => 0 [expires] => 0 [path] => / [domain] => )
- [1] => stdClass Object ( [cookies] => Array ( [III_EXPT_FILE] => aa2171 ) [extras] => Array ( ) [flags] => 0 [expires] => 0 [path] => / [domain] => .example.com )
- [2] => stdClass Object ( [cookies] => Array ( [III_SESSION_ID] => 193a3ce5aaadea85937c25cd0430332f ) [extras] => Array ( ) [flags] => 0 [expires] => 0 [path] => / [domain] => .example.com )
)
Now I need to copy these cookies over to the next outgoing request to the redirected location. I'm using HttpRequest->setCookies(), in which the argument is the array that was returned from the previous getResponseCookies() call.
What I see in the outgoing request is:
Cookie: 0%5Bcookies%5D%5BSESSION_SCOPE%5D=1; 0%5Bflags%5D=0; 0%5Bexpires%5D=0; 0%5Bpath%5D=%2F; 0%5Bdomain%5D=; 1%5Bcookies%5D%5BIII_EXPT_FILE%5D=aa2171; 1%5Bflags%5D=0; 1%5Bexpires%5D=0; 1%5Bpath%5D=%2F; 1%5Bdomain%5D=.example.com; 2%5Bcookies%5D%5BIII_SESSION_ID%5D=193a3ce5aaadea85937c25cd0430332f; 2%5Bflags%5D=0; 2%5Bexpires%5D=0; 2%5Bpath%5D=%2F; 2%5Bdomain%5D=.example.com
My questions are:
- What's the correct way of doing this? Because clearly the array indices are being added to the header too
- How can I prevent the url-encoding of the parameters?
- How can I prevent the 'path' and 'domain' attributes from being added to the header?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了。由于我固有的新手性格,我对第一个和第二个事务使用单独的 HttpRequest 对象。
相反,在创建第一个请求后,我只是调用 enableCookies() 方法< /a> 并重新使用相同的对象来发送第二个请求。
简而言之:
Solved it. In my inherent newbie-ness, I was using separate HttpRequest objects for the first and second transactions.
Instead, after creating the first request, I simply called the enableCookies() method and re-used the same object to send the second request.
In a nutshell: