无法模拟 POST 请求 -- 没有得到正确的响应
Unable to simulate a POST request -- not getting the correct response
我正在尝试通过 command-line NodeJS 脚本在远程站点上以编程方式提交表单(POST 请求)并抓取 return 数据。
远程形式为here。
当我通过浏览器提交它时,它首先转到页面本身(在 <form action>
中指定),其中 returns 一个 302 状态代码重定向到另一个页面,打印数据。
但是,当我通过 NodeJS 以编程方式发出 POST 请求时,我收到了 200 Server Busy
响应。我也在 PHP 中尝试过等效代码,但没有骰子。
我正在传递 headers、cookie 和表单数据以尝试模拟浏览器的请求,这些数据是从 Chrome 的网络检查器中复制的。
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)
//}
}
);
我很确定问题不在于 POST 数据中的希伯来语。我创建了一个测试服务器,它只打印了 headers 和 POST 数据,这段代码指向那里工作正常。
如何模拟这个请求?
更新:我尝试了来自不同域的其他一些 URL。 http://www.mop-zafon.org.il/csv/cgi-bin/picman.cgi works, while http://www.mop-zafon.net/DynamicTable.aspx?G_ID=0 没有。
使用 URL 查询字符串发出 POST 请求是否也有问题?
您是否在请求中发回 VIEWSTATE 字段?该站点似乎是在初始页面请求加密后发送给您的,并且可能包含 CSRF 保护。我会尝试让脚本最初启动一个真正的页面请求,获取 所有 的隐藏元素,然后提交回去看看你是否仍然得到 200 而不是 302。
原来需要User-Agent
header组。我猜它只是想发送到浏览器,而不是脚本。
我还需要包括 __VIEWSTATE
表单数据以及使用方法 suggested by Sean Baker。
最后,followAllRedirects: true
需要添加到选项 object 以使其跟随重定向。
我正在尝试通过 command-line NodeJS 脚本在远程站点上以编程方式提交表单(POST 请求)并抓取 return 数据。
远程形式为here。
当我通过浏览器提交它时,它首先转到页面本身(在 <form action>
中指定),其中 returns 一个 302 状态代码重定向到另一个页面,打印数据。
但是,当我通过 NodeJS 以编程方式发出 POST 请求时,我收到了 200 Server Busy
响应。我也在 PHP 中尝试过等效代码,但没有骰子。
我正在传递 headers、cookie 和表单数据以尝试模拟浏览器的请求,这些数据是从 Chrome 的网络检查器中复制的。
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)
//}
}
);
我很确定问题不在于 POST 数据中的希伯来语。我创建了一个测试服务器,它只打印了 headers 和 POST 数据,这段代码指向那里工作正常。
如何模拟这个请求?
更新:我尝试了来自不同域的其他一些 URL。 http://www.mop-zafon.org.il/csv/cgi-bin/picman.cgi works, while http://www.mop-zafon.net/DynamicTable.aspx?G_ID=0 没有。
使用 URL 查询字符串发出 POST 请求是否也有问题?
您是否在请求中发回 VIEWSTATE 字段?该站点似乎是在初始页面请求加密后发送给您的,并且可能包含 CSRF 保护。我会尝试让脚本最初启动一个真正的页面请求,获取 所有 的隐藏元素,然后提交回去看看你是否仍然得到 200 而不是 302。
原来需要User-Agent
header组。我猜它只是想发送到浏览器,而不是脚本。
我还需要包括 __VIEWSTATE
表单数据以及使用方法 suggested by Sean Baker。
最后,followAllRedirects: true
需要添加到选项 object 以使其跟随重定向。