phantomjs - Cookie is not being sent for any XHR/POST/GET AJAX requests

前端 未结 1 508
遇见更好的自我
遇见更好的自我 2021-01-17 10:51

I found an interesting issue when attempting to login using PhantomJS. I\'m at a loss as to why it\'s actually occurring.

Basically you start up a remote debugger li

相关标签:
1条回答
  • 2021-01-17 11:26

    Ok, this seems to be something related with session cookies and not regular cookies.

    Heres a huge thread on the developer in charge of the cookies feature of phantomjs and some guys with the same issue as yours.

    https://groups.google.com/forum/#!msg/phantomjs/2UbPkIibnDg/JLV9jBKxhIQJ

    If you dont want to skirm through the entire file, basically: Phantomjs behaves like a regular browser, and it deletes all session cookies when browser closes, in your case, when your script execution ends.

    So, even if you set the --cookies-file=/path/to/cookies.txt option you will only store regular cookies on it, for subsecuent executions.

    There are two possible approaches for you. One, is to make all requests within the same script, or the other is to store and restore the cookies manually.

    On the thread there are a couple of functions that you can use to do this.

    function saveCookies(sessionCookieFile, page) {
        var fs = require("fs");
        fs.write(sessionCookieFile, JSON.stringify(page.cookies));
    }
    
    function restoreCookies(sessionCookieFile, page) {
        var fs = require("fs");
        var cookies = fs.read(sessionCookieFile);
        page.cookies = JSON.parse(cookies);
    }
    
    var page = require('webpage').create();
    

    And, if everything fails...

    You could download source code and recopile phantomjs

    You will need to edit src/cookiejar.cpp and remove or comment purgeSessionCookies();

    Hope, this helps.

    0 讨论(0)
提交回复
热议问题