dongnaopa6200 2018-04-21 19:52
浏览 2649
已采纳

使用GuzzleHttp从HTTP调用获取cookie的值

I am now using GuzzleHttp to make HTTP requests, first I make a POST request to login.asp, which returns a response with Set-Cookie with a value that I need for future requests

enter image description here

When I inspect my obtained answer I get the following

enter image description here

As noted, I get all the keys except the Set-Cookie, what can be happening? How can I get this value? I'm using "guzzlehttp/guzzle": "^6.3", or can I get it using another tool?

    $jar = new CookieJar;

    $client = new Client([
        'base_uri' =>'miurl/',
        'timeout'  => 10.0,
        'cookies' => $jar
    ]);

    $response = $client->request('POST', 'login.asp',  [
        'form_params' => [
            'pws' => '',//data password
            'user' => '',//data user
        ]
    ]);

    //Request require coookies

    $response = $client->request('POST', 'goform/Wls',  [
        'form_params' => [
            /*Form´Params*/
        ],
       //if I manually add a correct userid the post application works fine
        'headers' => [
            //Require cookie param userid 
            'Cookie' => 'LANG_COOKIE=lang_span; userid=1524324306',
        ]
    ]);

Alternatively, I used this configuration without being able to obtain the cookie yet

checking a bit the answer using postman, is that after doing the correct login is still on the same page but with javascript redirect, can this influence?

<script language='JavaScript'>window.location='/admin/cable-Systeminfo.asp';</script>
</html>

The requests I make directly for a router hitron technologies cgnv22 to manage the mac filtering, I would like to provide more information but it is sensitive information

  • 写回答

1条回答 默认 最新

  • doujiaoang69440 2018-05-16 00:39
    关注

    It seems you're making the request the right way, passing an instance of CookieJarInterface. However, you shouldn't expect the Set-Cookie header. Instead, inspect your jar to check what cookies were returned.

    The following example shows how you can iterate over all cookies:

    $client = new \GuzzleHttp\Client();
    
    $jar = new \GuzzleHttp\Cookie\CookieJar();
    $request = $client->request('GET', 'https://www.google.com/', [
        'cookies' => $jar
    ]);
    
    $it = $jar->getIterator();
    while ($it->valid()) {
        var_dump($it->current());
        $it->next();
    }
    

    Here is a sample output from the snippet above:

    object(GuzzleHttp\Cookie\SetCookie)#36 (1) {
      ["data":"GuzzleHttp\Cookie\SetCookie":private]=>
      array(9) {
        ["Name"]=>
        string(3) "NID"
        ["Value"]=>
        string(132) "130=dmyl6v*******"
        ["Domain"]=>
        string(11) ".google.com"
        ["Path"]=>
        string(1) "/"
        ["Max-Age"]=>
        NULL
        ["Expires"]=>
        int(1542242169)
        ["Secure"]=>
        bool(false)
        ["Discard"]=>
        bool(false)
        ["HttpOnly"]=>
        bool(true)
      }
    }
    

    Refer to the CookieJar class source for more information on how you can access the returned cookies. Additionally, you can take a look at the docs for ArrayIterator class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 软件自定义无线电该怎样使用
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下