簡體   English   中英

無法模擬POST請求-無法獲得正確的響應

[英]Unable to simulate a POST request — not getting the correct response

我試圖以編程方式從命令行NodeJS腳本在遠程站點上提交表單(POST請求),然后抓取返回數據。

遠程表格在這里

當我通過瀏覽器提交它時,它首先進入頁面本身(在<form action>指定),該頁面返回302狀態代碼,重定向到另一個頁面,該頁面打印數據。

但是,當我通過NodeJS以編程方式發出POST請求時,得到了200 Server Busy響應。 我也嘗試過PHP中的等效代碼,但沒有骰子。

我正在傳遞標頭,Cookie和表單數據,以嘗試模擬從Chrome的網絡檢查器復制的瀏覽器的請求。

這是request模塊。

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數據的測試服務器,並且該代碼指向那里很好。

我如何模擬此請求?

更新:我嘗試了來自其他域的其他一些URL。 http://www.mop-zafon.org.il/csv/cgi-bin/picman.cgi有效,而http://www.mop-zafon.net/DynamicTable.aspx?G_ID=0則無效。

用URL查詢字符串發出POST請求是否有問題?

您是否正在根據請求發送VIEWSTATE字段? 該站點似乎是在加密的初始頁面請求上將其發送給您的,並且可能包含CSRF保護。 我會嘗試讓腳本最初啟動一個真正的頁面請求,獲取所有隱藏的元素,然后再次提交以查看是否仍然得到200而不是302。

原來,它需要User-Agent標頭集。 我猜它只想發送到瀏覽器,而不是腳本。

我還需要使用Sean Baker建議的方法包括__VIEWSTATE表單數據。

最后, followAllRedirects: true需要將followAllRedirects: true添加到options對象以使其遵循重定向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM