无法使用 Zend_Http_Client 存储第二个客户端请求的会话

发布于 2024-10-21 06:02:19 字数 666 浏览 3 评论 0原文

我正在向同一客户端发送多个请求。 zend_Http_Client 无法重定向,因为我们的网站提供了 javascript 重定向。从该 javascript 中,我获取了重定向 url 并再次调用客户端,现在我的访问被拒绝。这是因为我无法存储第一个客户端请求的会话。

我正在使用以下代码。

$client = new Zend_Http_Client(
        $loginUrl, 
        array(
            'keepalive' => true,
            'timeout'   => 1000
        )
    );
$response = $client->request()->getBody();
$redirectUrl = $this->_getResponseRedirectUrl($response);

$client->resetParameters();         
$client->setUri($redirectUrl);
$response = $client->request()->getBody();

$resultUrl = $this->_getResponseRedirectUrl($response);

任何人都可以告诉我如何存储第二个请求的会话。

I'm sending multiple requests to same client. zend_Http_Client not able to redirect because our site giving a javascript redirect. And from that javascript I'm getting the redirect url and again calling the client , now I'm getting access denied. This is beacuse I'm not able to store the session from the first client request.

I'm using the following code..

$client = new Zend_Http_Client(
        $loginUrl, 
        array(
            'keepalive' => true,
            'timeout'   => 1000
        )
    );
$response = $client->request()->getBody();
$redirectUrl = $this->_getResponseRedirectUrl($response);

$client->resetParameters();         
$client->setUri($redirectUrl);
$response = $client->request()->getBody();

$resultUrl = $this->_getResponseRedirectUrl($response);

Can anybody tell me how to store the session for the second request.

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

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

发布评论

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

评论(2

临风闻羌笛 2024-10-28 06:02:19

在尝试以下步骤之前,请确保您的服务器身份验证基于会话 cookie。

解决方案对我有用:

您需要将第一次休息中的cookie添加到第二次请求中。在这种情况下,服务器将您的第二个请求视为经过身份验证的请求。在登录站点后的响应中,您的 cookie 将具有以下一组值,例如 cookie-name、cookie-value、Expiration Date、Path、Secure 和 HTTPOnly。从 cookie 字符串中分解 cookie-name 和 cookie-value

登录响应后的示例会话 Cookie:

"JSESSIONID=DQl3NKXXmy3yntp3NW2GMlcn8pLn9PR9rl0lnR6vbtfdVpyHWdnq!598502565; path=/"
"CK_LanguageID_252085=1; expires=Friday, 23-Mar-2012 05:31:03 GMT; path=/; secure"
"CK_TimeZone_252085=4; expires=Friday, 23-Mar-2012 05:31:03 GMT; path=/; secure"

按照以下模式创建新的 cookie 字符串以用于进一步的服务器通信:

"JSESSIONID=DQl3NKXXmy3yntp3NW2GMlcn8pLn9PR9rl0lnR6vbtfdVpyHWdnq!598502565;  CK_LanguageID_252085=1; CK_TimeZone_252085=4"

添加一个方便的方法来从 zend_http_client“Set-cookie”数组创建 cookie 字符串。

/**
 * Get clean cookie string with only name,value pair
 * This method filer all the follwoing cookie information
 * Expiration Date, Path, Secure and HTTPOnly
 * @access public
 * @param {Array} $cookies
 * @return {String} $cookieString
 */
public function getCookieString(array $cookies){
  $cookieString = null;  
  foreach($cookies as $cookie){
      $part = explode(';',$cookie);
      $cookieString = ($cookieString == null)? $part[0] : $cookieString . '; ' . $part[0];
    }
  return $cookieString;
}

使用 Zend_Http_Client 发出连续请求:

 //Login
 $client = new Zend_Http_Client($loginUrl); 
 $response = $client->request();

 //Get Header from response
 $headers = $response->getHeaders();
 //Create second header 
 $header = array("Cookie" => $this->getCookieString($headers["Set-cookie"]));
 $client->setHeaders($header);

 //Second request
 $client->setUri($redirectUrl);
 $response = $client->request();  

这里我删除“$client->resetParameters();”因为您没有使用“$client->setParameterGet()”设置任何 GET 参数(POST 也相同)

如果使用“$client->setParameterGet()”或“$client->setParameterPost()”使用“$client->resetParameters();”在设置第二个 uri 之前。

$client->resetParameters() 接受布尔值:

  • FALSE :这是仅重置 POST 和 GET 参数的默认值。
  • TRUE :重置所有参数,包括标头、上次请求和上次响应。

Before trying the following steps make sure your server authentication is based on session cookies.

Solution Worked for me:

You need to add cookies from the first repose to the second request. In this case server consider your second request as authenticated one.In the response after login to the site your cookie will be having following set of values like cookie-name,cookie-value,Expiration Date, Path, Secure and HTTPOnly. From the cookie string explode cookie-name and cookie-value

Example Session Cookie after login response:

"JSESSIONID=DQl3NKXXmy3yntp3NW2GMlcn8pLn9PR9rl0lnR6vbtfdVpyHWdnq!598502565; path=/"
"CK_LanguageID_252085=1; expires=Friday, 23-Mar-2012 05:31:03 GMT; path=/; secure"
"CK_TimeZone_252085=4; expires=Friday, 23-Mar-2012 05:31:03 GMT; path=/; secure"

Create new cookie string for further server communication in the following pattern:

"JSESSIONID=DQl3NKXXmy3yntp3NW2GMlcn8pLn9PR9rl0lnR6vbtfdVpyHWdnq!598502565;  CK_LanguageID_252085=1; CK_TimeZone_252085=4"

Adding a handy method to create cookie string from zend_http_client "Set-cookie" array.

/**
 * Get clean cookie string with only name,value pair
 * This method filer all the follwoing cookie information
 * Expiration Date, Path, Secure and HTTPOnly
 * @access public
 * @param {Array} $cookies
 * @return {String} $cookieString
 */
public function getCookieString(array $cookies){
  $cookieString = null;  
  foreach($cookies as $cookie){
      $part = explode(';',$cookie);
      $cookieString = ($cookieString == null)? $part[0] : $cookieString . '; ' . $part[0];
    }
  return $cookieString;
}

Using Zend_Http_Client making consecutive requests:

 //Login
 $client = new Zend_Http_Client($loginUrl); 
 $response = $client->request();

 //Get Header from response
 $headers = $response->getHeaders();
 //Create second header 
 $header = array("Cookie" => $this->getCookieString($headers["Set-cookie"]));
 $client->setHeaders($header);

 //Second request
 $client->setUri($redirectUrl);
 $response = $client->request();  

Here I am removing "$client->resetParameters();" because you are not setting any GET Params using "$client->setParameterGet()"(Same for POST as well)

If using "$client->setParameterGet()" or "$client->setParameterPost()" use "$client->resetParameters();" before setting second uri.

$client->resetParameters() accepts boolean values:

  • FALSE : This is the default value which reset POST and GET Params only.
  • TRUE : Reset all the params including headers,last request and last response.
翻身的咸鱼 2024-10-28 06:02:19

实际上,Zend_Http_Client 提供了一种非常简单的方法来完成您所做的事情,无需在这里重新发明轮子。

$client = new Zend_Http_Client(
        $loginUrl, 
        array(
            'keepalive' => true,
            'timeout'   => 1000
        )
    );

// this is the magic line...
$client->setCookieJar();

$response = $client->request()->getBody();
$redirectUrl = $this->_getResponseRedirectUrl($response);

$client->resetParameters();         
$client->setUri($redirectUrl);
$response = $client->request()->getBody();

$resultUrl = $this->_getResponseRedirectUrl($response);

在这里阅读更多内容: http://framework.zend.com/manual/en /zend.http.cookies.html

Actuall, Zend_Http_Client provides a very simple way to do what you've done, no need to reinvent the wheel here.

$client = new Zend_Http_Client(
        $loginUrl, 
        array(
            'keepalive' => true,
            'timeout'   => 1000
        )
    );

// this is the magic line...
$client->setCookieJar();

$response = $client->request()->getBody();
$redirectUrl = $this->_getResponseRedirectUrl($response);

$client->resetParameters();         
$client->setUri($redirectUrl);
$response = $client->request()->getBody();

$resultUrl = $this->_getResponseRedirectUrl($response);

Read more here: http://framework.zend.com/manual/en/zend.http.cookies.html

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