我正在使用phantomjs模拟网站上的人机交互,以从中获取一些数据。 该页面有一个表单->我用phantomjs填充了表单->我单击了提交按钮。 提交过程如下:触发POST请求,此请求重定向到带有空标头的GET请求。但是,当我在浏览器中手动测试时,第二个请求(GET)具有标头。
我们没有从GET请求中收到所需的响应,因为它没有任何标头:User-Agent,Cookie等。
我试图用两种方法添加标题,但没有成功:
1。
page.onResourceRequested = function (request, networkRequest) {
if(request.id == '28' && request.headers.length == 0) {
var cookies = '';
for (var i = 0; i<page.cookies.length; i++) {
cookies += page.cookies[i].name + "=" + page.cookies[i].value + '; ';
}
request.headers = [
{
"name": "User-Agent",
"value": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36"
},
{
"name": "Accept",
"value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
},
{
"name": "Cache-Control",
"value": "max-age=0"
},
{
"name": "Connection",
"value": "keep-alive"
},
{
"name": "Content-Type",
"value": "application/x-www-form-urlencoded"
},
{
"name": "Cookie",
"value": cookies
}
];
}
};
2。
page.onResourceRequested = function (request, networkRequest) {
if(request.id == '28' && request.headers.length == 0) {
var cookies = '';
for (var i = 0; i<page.cookies.length; i++) {
cookies += page.cookies[i].name + "=" + page.cookies[i].value + '; ';
}
networkRequest.setHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36");
networkRequest.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
networkRequest.setHeader("Cache-Control", "max-age=0");
networkRequest.setHeader("Connection", "keep-alive");
networkRequest.setHeader("Content-Type", "Content-Type");
networkRequest.setHeader("Cookie", cookies);
}
};
我打印了请求,它看起来像:
{
"headers": [],
"id": 28,
"method": "GET",
"time": "2019-02-07T08:11:59.429Z",
"url": "[THE GET URL]"
}
我正在使用phantomjs 2.5.0(这是唯一适用于我的库)。
如何为该GET请求成功向标头添加cookie?
谢谢。