gpt4 book ai didi

javascript - phantomjs 使用 formurlencoded 发布请求

转载 作者:行者123 更新时间:2023-11-28 05:40:24 24 4
gpt4 key购买 nike

我是 phantomjs 的新手,我编写了一段代码,让我可以使用 header 和正文发出发布请求。但是,当我发出发布请求时,数据不会发送。有什么问题吗?

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();
});

这是我使用 fiddler 跟踪请求时得到的屏幕截图,结果如下。在我的标题中,一切都在那里: Header

但是在网络表单中我什么也没得到:Webform

然后我收到 500 内部服务器错误。

我需要更改什么才能发出发布请求,以便将数据包含在我的发布请求中?

我也需要 cookies 。如果没有 cookie,我将无法登录。这就是我想出的你的代码片段。但由于某种原因它无法正常工作。

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();
});

我怎样才能做到这一点?

});

最佳答案

我尝试了你的代码示例,它确实无法工作(PhantomJS 2.1.1)。

但是回落到 the old way是否发送数据:

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();
});

服务器接收数据:

array(4) {
["__RequestVerificationToken"]=>
string(172) "Ujol0sctq8I5bko4eLieDLP6ZiX1wShq3zP77JLpj8rAJy0MJIaI9JAt-ZfYmmqQkiL9QVUw7la6lqlwByqcUifU264H35Dq0pZUokl87jc5f1SpNA4G1_y5sF2eWB-MKWDC5WW33GdZnyRRXZP2ndGcBFB0hTwk5SPhGc3-z1I1"
["ReturnUrl"]=>
string(0) ""
["Email"]=>
string(16) "111111@gmail.com"
["Password"]=>
string(6) "111111"
}

编辑 1

I need the cookies also. I will not be able to login without the cookies.

如果您使用命令开关 --cookies-file 启动 PhantomJS,PhantomJS 能够自动处理 cookie:

/usr/bin/phantomjs --cookies-file=/home/usr/cookies.txt /home/usr/script.js

关于javascript - phantomjs 使用 formurlencoded 发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38965213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com