将 cookie 从第一个响应复制到下一个请求

发布于 2024-10-03 14:54:41 字数 1645 浏览 2 评论 0原文

我使用 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

我的问题是:

  1. 这样做的正确方法是什么?因为显然数组索引也被添加到标头中,
  2. 如何防止参数的 url 编码?
  3. 如何防止将“路径”和“域”属性添加到标头?

谢谢!

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:

  1. What's the correct way of doing this? Because clearly the array indices are being added to the header too
  2. How can I prevent the url-encoding of the parameters?
  3. How can I prevent the 'path' and 'domain' attributes from being added to the header?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尤怨 2024-10-10 14:54:41

解决了。由于我固有的新手性格,我对第一个和第二个事务使用单独的 HttpRequest 对象。

相反,在创建第一个请求后,我只是调用 enableCookies() 方法< /a> 并重新使用相同的对象来发送第二个请求。

简而言之:

$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! */

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! */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文