乱码而不是html响应作为node.request中的主体

gar*_*son 0 curl http node.js

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

??Z?r?H ?? c ????? pT ???? $ 3 ???〜Y?@?MK8 ???> * ???? z)?U ????? J? ???? tB ?? x ?? c ?????????????? 0?H ?????? V ?? O'?7 ????} ??? L? “?} / ta?xn?g#??? O ??????

有什么想法可能导致此问题或如何解决?

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

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

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
Run Code Online (Sandbox Code Playgroud)

gar*_*son 5

我想到了!这种胡言乱语是由于我的request()缺少'gzip:true'选项引起的。现在,第二个请求显示为:

url: urlstring2
    ,gzip: true
    ,headers:{...
Run Code Online (Sandbox Code Playgroud)