2015-02-09 120 views
-1

我试图以编程方式从命令行NodeJS脚本提交表单(POST请求)在远程站点上,并刮擦返回数据。无法模拟POST请求 - 没有得到正确的响应

远程表格是here

当我通过浏览器提交它时,它首先进入页面本身(在<form action>中指定),该页面返回302状态码重定向到打印数据的不同页面。

但是,当我通过NodeJS以编程方式发出POST请求时,我得到了一个200 Server Busy响应。我也尝试了PHP中的等效代码,但没有骰子。

我传递标题,Cookie和表单数据以尝试模拟浏览器的请求,从Chrome的网络检查器复制。

This is the request module.

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数据。我创建了一个只打印头文件和POST数据的测试服务器,并且此代码正常工作。

如何模拟此请求?

更新:我尝试了其他域的其他一些网址。 http://www.mop-zafon.org.il/csv/cgi-bin/picman.cgi作品,而http://www.mop-zafon.net/DynamicTable.aspx?G_ID=0没有。

是否有可能使用URL查询字符串进行POST请求也是一个问题?

+1

为什么downvote?这似乎是一个经过深入研究的问题。 – Scimonster 2015-02-10 21:18:44

+0

对于这样的事情来说,一个非常有用的工具是Fiddler。它可以让你查看来自浏览器的请求/响应,修改请求,并通过你喜欢的任何调整发送出去。您将使用'Inspector> Raw'和'Composer> Raw'选项卡。 – Pluto 2015-02-11 16:47:03

回答

1

事实证明,它需要User-Agent标题集。我想它只是想发送到浏览器,而不是脚本。

我还需要使用方法suggested by Sean Baker包含__VIEWSTATE表单数据。

最后,需要将followAllRedirects: true添加到选项对象以使其遵循重定向。

1

您是否在请求上发送VIEWSTATE字段?该网站似乎在加密的初始页面请求中将其发送给您,并且可能包含CSRF保护。我试着让脚本最初发起一个真正的页面请求,抓住所有的的隐藏元素,然后提交回来,看看你是否仍然得到200而不是302.

+0

我已经授予你赏金,因为你让我解决问题。它还需要更多的东西,所以我张贴了我自己的答案,并接受了一个。 – Scimonster 2015-02-23 12:59:11