代码之家  ›  专栏  ›  技术社区  ›  Scimonster

无法模拟POST请求--未得到正确的响应

  •  0
  • Scimonster  · 技术社区  · 10 年前

    我试图通过命令行NodeJS脚本在远程站点上以编程方式提交表单(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数据,这段代码在这里运行得很好。

    如何模拟此请求?

    更新:我尝试了其他一些来自不同域的URL。 http://www.mop-zafon.org.il/csv/cgi-bin/picman.cgi 工作,同时 http://www.mop-zafon.net/DynamicTable.aspx?G_ID=0 没有。

    使用URL查询字符串发出POST请求是否也有问题?

    2 回复  |  直到 10 年前
        1
  •  1
  •   Sean Baker    10 年前

    是否将VIEWSTATE字段发送回请求?该网站似乎是在加密的初始页面请求中将其发送给您,并且可能包含CSRF保护。我会尝试让脚本最初启动一个真正的页面请求 全部的 然后提交回来,看看您是否仍然获得200而不是302。

        2
  •  1
  •   Community CDub    7 年前

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

    我还需要包括 __VIEWSTATE 也使用该方法形成数据 suggested by Sean Baker .

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