Javascript 无法模拟POST请求--未获得正确的响应
我试图通过命令行NodeJS脚本在远程站点上以编程方式提交表单(POST请求),并获取返回数据 远程表单是 当我通过浏览器提交时,它首先进入页面本身(在Javascript 无法模拟POST请求--未获得正确的响应,javascript,node.js,post,remote-access,Javascript,Node.js,Post,Remote Access,我试图通过命令行NodeJS脚本在远程站点上以编程方式提交表单(POST请求),并获取返回数据 远程表单是 当我通过浏览器提交时,它首先进入页面本身(在中指定),返回一个302状态代码,重定向到另一个页面,打印数据 然而,当我通过NodeJS以编程方式发出POST请求时,我会得到一个200服务器忙的响应。我也尝试过PHP中的等效代码,但没有成功 我正在传递头、cookie和表单数据,以尝试模拟浏览器的请求,这些请求是从Chrome的网络检查器复制的 我很确定,问题不在于POST数据中的希伯来语
中指定),返回一个302状态代码,重定向到另一个页面,打印数据
然而,当我通过NodeJS以编程方式发出POST请求时,我会得到一个200服务器忙的响应。我也尝试过PHP中的等效代码,但没有成功
我正在传递头、cookie和表单数据,以尝试模拟浏览器的请求,这些请求是从Chrome的网络检查器复制的
我很确定,问题不在于POST数据中的希伯来语。我创建了一个测试服务器,它只打印标题和POST数据,代码在这里运行良好
如何模拟此请求?
更新:我尝试了其他一些来自不同域的URL。行得通,不行
使用URL查询字符串发出POST请求是否可能有问题?是否将VIEWSTATE字段发送回请求?该网站似乎在发送给您的初始页面请求加密,它可能包含CSRF保护。我会尝试让脚本一开始启动一个真正的页面请求,抓取所有隐藏元素,然后提交回去,看看您是否仍然得到200而不是302。结果是它需要设置用户代理
标题。我猜它只想发送到浏览器,而不是脚本
我还需要使用该方法包含\uu VIEWSTATE
表单数据
最后,followAllRedirects:true
需要添加到options对象中,使其跟随重定向。为什么要投否决票?这似乎是一个研究得很好的问题。一个真正有用的工具是小提琴手。它允许您查看来自浏览器的请求/响应,修改请求,并通过任意调整发送它们。你将使用Inspector>Raw
和Composer>Raw
选项卡。我授予你奖金,因为你让我走上了解决问题的道路。它还需要更多的东西,所以我发布了自己的答案并接受了。
var url = 'http://www.meteo.co.il/StationReportFast.aspx?ST_ID=120';
var request = require('request');
var jar = request.jar();
jar.setCookie(request.cookie("ASP.NET_SessionId=tsytqpkr04g5w2bfsu3fncbx"), url);
jar.setCookie(request.cookie("arp_scroll_position=177"), url);
//console.log(jar)
request.post(
url, {
form: {
'__EVENTTARGET' : '',
'__EVENTARGUMENT' : '',
'chkAll' : 'on',
'lstMonitors' : '%3CWebTree%3E%3CNodes%3E%3ClstMonitors_1%20Checked%3D%22true%22%3E%3C/lstMonitors_1%3E%3ClstMonitors_2%20Checked%3D%22true%22%3E%3C/lstMonitors_2%3E%3ClstMonitors_3%20Checked%3D%22true%22%3E%3C/lstMonitors_3%3E%3ClstMonitors_4%20Checked%3D%22true%22%3E%3C/lstMonitors_4%3E%3ClstMonitors_5%20Checked%3D%22true%22%3E%3C/lstMonitors_5%3E%3ClstMonitors_6%20Checked%3D%22true%22%3E%3C/lstMonitors_6%3E%3ClstMonitors_7%20Checked%3D%22true%22%3E%3C/lstMonitors_7%3E%3ClstMonitors_8%20Checked%3D%22true%22%3E%3C/lstMonitors_8%3E%3ClstMonitors_9%20Checked%3D%22true%22%3E%3C/lstMonitors_9%3E%3ClstMonitors_10%20Checked%3D%22true%22%3E%3C/lstMonitors_10%3E%3ClstMonitors_11%20Checked%3D%22true%22%3E%3C/lstMonitors_11%3E%3ClstMonitors_12%20Checked%3D%22true%22%3E%3C/lstMonitors_12%3E%3ClstMonitors_13%20Checked%3D%22true%22%3E%3C/lstMonitors_13%3E%3ClstMonitors_14%20Checked%3D%22true%22%3E%3C/lstMonitors_14%3E%3C/Nodes%3E%3C/WebTree%3E',
'RadioButtonList1' : '0',
'RadioButtonList2' : '0',
'BasicDatePicker1$TextBox' : '02/02/2015',
'txtStartTime' : '00:00',
'txtStartTime_p' : '2015-2-3-0-0-0-0',
'BasicDatePicker2$TextBox' : '03/02/2015',
'txtEndTime' : '00:00',
'txtEndTime_p' : '2015-2-3-0-0-0-0',
'ddlAvgType' : 'AVG',
'ddlTimeBase' : '60',
'btnGenerateReport' : 'הצג דוח',
'txtErrorMonitor' : 'אנא בחר לפחות מוניטור אחד',
'txtErrorTimeBase' : 'בחר בסיס זמן',
'txtError2Y' : 'Select2Monitors'
},
jar: jar,
headers: {
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
Host: 'www.meteo.co.il',
Origin: 'http://www.meteo.co.il',
Referer: 'http://www.meteo.co.il/StationReportFast.aspx?ST_ID=120',
'Content-Type': 'application/x-www-form-urlencoded'
}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
} //else {
console.log(arguments)
//}
}
);