在node.request中将gibberish而不是html响应作为body

时间:2017-06-30 22:21:52

标签: node.js http curl

我正在使用npm中的请求模块向受密码保护的站点发出HTTP请求,输入密码,存储cookie,然后在存储和验证cookie后发出请求。我能够像普通的浏览器请求那样得到相同的标题,但是它本身,而不是我在浏览器中获得的HTML文档,只是看起来像这样:

Z r H c 䞙 T ƾ $3 ƾ ~Y @ MK8 > * ? z) ?U ݨĴ혳섯tBxc?0HVO'7} L“˖} / taxng#ݱ0

任何可能导致此问题或我如何解决问题的想法?

此外,当我从命令提示符运行时,计算机“dings”

这是我正在运行的完整node.js代码(减去URL /密码等)

var parsedurl1 = url.parse( urlstring1 );
var options1 = {
  hostname: parsedurl1.hostname,
  port: ( parsedurl1.port || 80 ), // 80 by default
  method: 'POST',
  path: parsedurl1.path,
  headers: { 
      'Host': hostname
      ,'User-Agent': myuseragent
      ,'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
      ,'Accept-Language':"en-US,en;q=0.5"
      ,'Accept-Encoding': "gzip, deflate"
      ,'Referer': hostname
      ,'Content-Type': "text/html; charset=utf-8"
      ,'Content-Length': Buffer.byteLength(postData)
      ,'Connection': "keep-alive"
      ,'Upgrade-Insecure-Requests': "1"},
};
var cookiefile ; 
var postData=querystring.stringify({
        'email': myemail
        ,'password':mypassword
        ,'action': 'login'
        ,'go.x': 0
        ,'go.y': 0
    })
var cookiefile;
var callback =  function ( response ) {
    // display returned cookies in header
    var setcookie = response.headers["set-cookie"];
    if ( setcookie ) {
      setcookie.forEach(
        function ( cookiestr ) {
             cookiefile = cookiestr;
             fs.writeFile(cookiefilelocation, cookiestr);
          console.log(  "COOKIE:" + cookiestr );
        }
      );
    }    
    var data = "";
    response.on(
      "data",
      function ( chunk ) { data += chunk; }
    );

    response.on(
      "end",
      function () {  
            newcookiefile =  cookiefile.substr(0, cookiefile.indexOf(";"));
            var parsedurl2 = url.parse( urlstring2 );
            var options2 = {
                url: urlstring2,
                // port: ( parsedurl2.port || 80 ), // 80 by default
                method: 'GET',
                // path: parsedurl2.path,
                headers: { 
                "Host": hostname
                ,"User-Agent": myuseragent
                ,'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
                ,'Accept-Language':"en-US,en;q=0.5"
                ,'Accept-Encoding': "gzip, deflate"
                ,'Referer':hostname
                ,'Cookie': newcookiefile
                ,'Connection': "keep-alive"
                ,'Upgrade-Insecure-Requests': "1"},
            };

            function callback3(error, response, body){
                console.log('error:', error); // Print the error if one occurred 

                console.log('statusCode:', response.headers ); // Print the response status code if a response was received 
                console.log('body:', body.substr(1,1000));
                fs.writeFile('./config/pullfiles/mostrecent.txt', body);
                }
            requestlib(options2, callback3);
      }
    );
  };
var request = http.request(options1, callback);


request.on(
  "error",
  function( err ) {
    console.error( "RERROR:" + err );
  }
);
request.write(postData);
request.end(); // let request know it is finished sending

1 个答案:

答案 0 :(得分:4)

我明白了!这种乱码是由于我缺少'gzip:true'选项是我的请求()。现在第二个请求是:

url: urlstring2
    ,gzip: true
    ,headers:{...