首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PhantomJS向请求添加了多个cookie

PhantomJS向请求添加了多个cookie
EN

Stack Overflow用户
提问于 2019-04-01 10:50:31
回答 1查看 376关注 0票数 0

我在向请求中添加多个cookie时遇到了问题,这是我的1 cookie的javascript:

代码语言:javascript
复制
var page = require('webpage').create();

phantom.addCookie({

  'name'     : 'TestCookie_Name_201312174009',   /* required property */
  'value'    : 'TestCookie_Value_164009',  /* required property */
  'domain'   : 'www.miau.com',        /* required property */
  'path'     : '/',
  'httponly' : true,
  'secure'   : false,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60)   /* <-- expires in 1 hour */
});



page.open('http://www.miau.com', function() {
    setTimeout(function() {
        //page.render('google.png');
        phantom.exit();
    }, 200);
});

我通过代理启动它来查看请求:

phantomjs --ignore-ssl-errors=true --disk-cache=true --proxy=http://127.0.0.1:8080 --web-security=false test.js

cookie添加得很好,但是接下来我尝试了两个cookie:

它不起作用,我也尝试过另一种选择,把它当作一张清单

代码语言:javascript
复制
var page = require('webpage').create();

phantom.addCookie([{

  'name'     : 'TestCookie_Name_201312174009',   /* required property */
  'value'    : 'TestCookie_Value_164009',  /* required property */
  'domain'   : 'www.miau.com',        /* required property */
  'path'     : '/',
  'httponly' : true,
  'secure'   : false,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60)   /* <-- expires in 1 hour */
}
,
{
  'name'     : 'TestCookie_Name_2',   /* required property */
  'value'    : 'TestCookie_Value_2',  /* required property */
  'domain'   : 'www.miau.com',        /* required property */
  'path'     : '/',
  'httponly' : true,
  'secure'   : false,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60)   /* <-- expires in 1 hour */
}]);

但我还是没办法让它起作用..。

我试过的另一件事是:

代码语言:javascript
复制
var page = require('webpage').create();

phantom.addCookie({

  'name'     : 'TestCookie_Name_201312174009',   /* required property */
  'value'    : 'TestCookie_Value_164009',  /* required property */
  'domain'   : 'www.miau.com',        /* required property */
  'path'     : '/',
  'httponly' : true,
  'secure'   : false,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60)   /* <-- expires in 1 hour */
});

phantom.addCookie({

  'name'     : 'TestCookie_Name_2',   /* required property */
  'value'    : 'TestCookie_Value_2',  /* required property */
  'domain'   : 'www.miau.com',        /* required property */
  'path'     : '/',
  'httponly' : true,
  'secure'   : false,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60)   /* <-- expires in 1 hour */
});

page.open('http://www.miau.com', function() {
    setTimeout(function() {
        //page.render('google.png');
        phantom.exit();
    }, 200);
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-01 11:15:20

查看PhantomJS文档:

addCookie(Object) {Boolean} 介绍: PhantomJS 1.7 在CookieJar中添加一个Cookie。如果成功添加,则返回true,否则返回false

它指的不是一个以上的曲奇。因此,看看phantom.cookies,它是一个变量,它保存了cookiejar -我们发现如下:

phantom.cookies {Object[]} 介绍: PhantomJS 1.7 为任何域获取或设置Cookies (不过,对于设置,最好使用phantom.addCookie )。这些Cookies存储在CookieJar中,并将在打开相关WebPages时提供。 该数组将由存储在PhantomJS启动配置/命令行选项(如果有的话)中指定的Cookie文件中的任何现有cookie数据预先填充。

上面的文档引号告诉我们,幻影对象内部的cookie变量是一个对象数组。因此,必须有可能分配多个。通过快速查看测试,我们注意到,有一个用于分配多个cookie的测试--参见github引用的代码行。

Cookie阵列

Cookies被分配

这基本上告诉我们,只需调用就可以分配多个cookie:

代码语言:javascript
复制
phantom.cookies = [{

  'name'     : 'TestCookie_Name_201312174009',   /* required property */
  'value'    : 'TestCookie_Value_164009',  /* required property */
  'domain'   : 'www.miau.com',        /* required property */
  'path'     : '/',
  'httponly' : true,
  'secure'   : false,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60)   /* <-- expires in 1 hour */
}
,
{
  'name'     : 'TestCookie_Name_2',   /* required property */
  'value'    : 'TestCookie_Value_2',  /* required property */
  'domain'   : 'www.miau.com',        /* required property */
  'path'     : '/',
  'httponly' : true,
  'secure'   : false,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60)   /* <-- expires in 1 hour */
}];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55453384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档