2015-02-09 125 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