[英]phantomjs post request with formurlencoded
I am new to phantomjs and I wrote a snippet of code that lets me make a post request with header and body. 我是phantomjs的新手,我写了一段代码,让我用标题和正文发表帖子请求。 However when I make the post request the data does not get sent.
但是,当我发布请求时,数据不会被发送。 What is the problem?
问题是什么?
var webPage = require('webpage');
var page = webPage.create();
console.log('1');
var settings = {
operation: "POST",
encoding: "utf8",
headers: {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
"Cookie":"ASP.NET_SessionId=ajq11hy45hmdmr51ra3ivtee; otohitsforgery=G6ZHZo_p9dC1oczq_GB8_9I76pgnmBoVbCpkKnG8oAJJLaEXhTlAJmsCT6Ttg5J_I4ajeODlp1OgksJ7xn1ZDTXLk85cr2vOxJ3kl5YsuCgmprP3WRNzm_f5wOJqgQNXLXwwdeBaxWqYeGi6PeQgbg2",
"Content-Type":"application/x-www-form-urlencoded"
},
data: {
"__RequestVerificationToken":"Ujol0sctq8I5bko4eLieDLP6ZiX1wShq3zP77JLpj8rAJy0MJIaI9JAt-ZfYmmqQkiL9QVUw7la6lqlwByqcUifU264H35Dq0pZUokl87jc5f1SpNA4G1_y5sF2eWB-MKWDC5WW33GdZnyRRXZP2ndGcBFB0hTwk5SPhGc3-z1I1",
"ReturnUrl":"",
"Email":"111111@gmail.com",
"Password":"111111"
}
};
page.open('url', settings, function(status) {
console.log('Status: ' + status);
var cookies = page.cookies;
console.log('Listing cookies:');
for(var i in cookies) {
console.log(cookies[i].name + '=' + cookies[i].value);
}
var content = page.content;
console.log('Content: ' + content);
phantom.exit();
});
Here is a screenshot when I track the request with fiddler I get the following. 这是我用fiddler跟踪请求的屏幕截图,我得到以下内容。 In my header everything is there: Header
在我的标题中,一切都在那里: 标题
However in the webform I get nothing: Webform 但是在webform中我什么都没得到: Webform
And then I get a 500 internal server error. 然后我得到500内部服务器错误。
What do I need to change in order to make a post request so that the data is included in my post request? 为了发布帖子请求,我需要更改什么才能将数据包含在我的帖子请求中?
I need the cookies also. 我也需要饼干。 I will not be able to login without the cookies.
没有cookie我将无法登录。 heres what I came up with your code snippet.
下面是我提出的代码片段。 But for some reason it is not working properly.
但由于某种原因,它无法正常工作。
var webPage = require('webpage');
var page = webPage.create();
console.log('1');
page.open("http://someurl.com",function(status){
var cookies = page.cookies;
console.log('Listing cookies:');
for(var i in cookies) {
console.log(cookies[i].name + '=' + cookies[i].value);
}
cookies = cookies[1].name + '=' + cookies[1].value + '; ' + cookies[0].name + '=' + cookies[0].value;
console.log(cookies);
var settings = {
operation: "POST",
encoding: "utf8",
headers: {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
"Cookie":cookies,
"Content-Type":"application/x-www-form-urlencoded"
},
data: {
"__RequestVerificationToken":"Ujol0sctq8I5bko4eLieDLP6ZiX1wShq3zP77JLpj8rAJy0MJIaI9JAt-ZfYmmqQkiL9QVUw7la6lqlwByqcUifU264H35Dq0pZUokl87jc5f1SpNA4G1_y5sF2eWB-MKWDC5WW33GdZnyRRXZP2ndGcBFB0hTwk5SPhGc3-z1I1",
"ReturnUrl":"",
"Email":"111111@gmail.com",
"Password":"111111"
}
var postBody = "";
Object.keys(data).forEach(function (key) {
postBody += (key + "=" + data[key] + "&");
});
console.log(postBody);
};
page.open('http://www.someurl.com', 'POST', settings, function(status) {
console.log('Status: ' + status);
phantom.exit();
});
How can I make this work? 我怎样才能做到这一点?
});
I tried your code sample and it indeed won't work (PhantomJS 2.1.1). 我尝试了你的代码示例,它确实不起作用(PhantomJS 2.1.1)。
However falling back to the old way does send data: 但是回到旧的方式会发送数据:
var data = {
"__RequestVerificationToken":"Ujol0sctq8I5bko4eLieDLP6ZiX1wShq3zP77JLpj8rAJy0MJIaI9JAt-ZfYmmqQkiL9QVUw7la6lqlwByqcUifU264H35Dq0pZUokl87jc5f1SpNA4G1_y5sF2eWB-MKWDC5WW33GdZnyRRXZP2ndGcBFB0hTwk5SPhGc3-z1I1",
"ReturnUrl":"",
"Email":"111111@gmail.com",
"Password":"111111"
}
var postBody = "";
Object.keys(data).forEach(function (key) {
postBody += (key + "=" + data[key] + "&");
});
console.log(postBody);
page.open('http://example.com/', 'POST', postBody, function(status) {
console.log('Status: ' + status);
phantom.exit();
});
The server receives data: 服务器接收数据:
array(4) {
["__RequestVerificationToken"]=>
string(172) "Ujol0sctq8I5bko4eLieDLP6ZiX1wShq3zP77JLpj8rAJy0MJIaI9JAt-ZfYmmqQkiL9QVUw7la6lqlwByqcUifU264H35Dq0pZUokl87jc5f1SpNA4G1_y5sF2eWB-MKWDC5WW33GdZnyRRXZP2ndGcBFB0hTwk5SPhGc3-z1I1"
["ReturnUrl"]=>
string(0) ""
["Email"]=>
string(16) "111111@gmail.com"
["Password"]=>
string(6) "111111"
}
Edit 1 编辑1
I need the cookies also.
我也需要饼干。 I will not be able to login without the cookies.
没有cookie我将无法登录。
PhantomJS is able to process cookies automatically if you launch it with command switch --cookies-file: 如果您使用命令开关--cookies-file启动它,PhantomJS能够自动处理cookie:
/usr/bin/phantomjs --cookies-file=/home/usr/cookies.txt /home/usr/script.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.