将Cookie从第一个响应复制到下一个请求 [英] Copying cookies from first response to next request
问题描述
我使用HttpRequest-> send()发送第一个HTTP请求,我收到一个带有以下Set-Cookie头的302响应:
- 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 = /
当我使用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] => ] => Array()[flags] => Array()=> )
现在,我需要将这些cookie复制到下一个对重定向位置的传出请求。我使用HttpRequest-> setCookies(),其中参数是从上一次getResponseCookies()调用返回的数组。
请求是:
Cookie:0%5Bcookies%5D%5BSESSION_SCOPE%5D = 1; 0%5Bflags%5D = 0; 0%5Bexpires%5D = 0; 0%5B径%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%5B path%5D =%2F; 2%5Bdomain%5D = .example.com
我的问题是:
- 这样做的正确方法是什么?
- 如何防止参数的url编码?
- 如何防止'path'和'domain'属性被添加到标题?
谢谢!
解决了。在我固有的新手,我使用单独的HttpRequest对象的第一和第二个事务。
而是,创建第一个请求后,我简单地调用 enableCookies()方法,并重新使用相同的对象来发送第二个请求
简而言之:
$ URL1 = );
/ *构造并发送第一个请求* /
$ r1 = new HttpRequest($ URL1,METH_POST);
$ r1-> enableCookies();
$ r1-> setPostFields(...);
$ r1-> send();
/ *验证响应实际上是302第一! * /
$ URL2 = $ URL1。 $ r1-> getResponseHeader(Location);
/ *构造并发送第二个请求* /
$ r1 = new HttpRequest($ URL2,METH_POST);
$ r1-> send();
/ *成功! * /
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!
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:
$URL1 = (main url);
/* Construct and send the first request */
$r1 = new HttpRequest ($URL1, METH_POST);
$r1->enableCookies();
$r1->setPostFields (...);
$r1->send();
/* Verify that the response is in fact a 302 first! */
$URL2 = $URL1 . $r1->getResponseHeader("Location");
/* Construct and send the second request */
$r1 = new HttpRequest ($URL2, METH_POST);
$r1->send();
/* Success! */
这篇关于将Cookie从第一个响应复制到下一个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!