2017-06-30 78 views
-2

我正在使用npm中的請求模塊向密碼保護的站點發出HTTP請求,放入密碼,存儲cookie,然後一旦cookie發出請求就發出請求存儲和驗證。我能夠獲得與我在普通瀏覽器請求中相同的頭文件,但是它本身並不是我在瀏覽器中獲得的HTML文檔,只是看起來像這樣:亂碼而不是html響應作爲node.request中的主體

Z r H c 䞙 pT Ī$3 ƾ 〜Y @ MK8 > * z) ݨ 혳 섯tB XC?0HVO'7} L「˖}/TA板塊漲幅居前#ݱØ

任何想法什麼可能導致這樣或如何解決它?

此外,當我運行這個從命令提示符,計算機「鐘聲「

這裏是我運行完整的Node.js代碼(減去網址/密碼的/ etc。)

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 
+3

您的計算機因爲包含「0x07」的字節,即「鈴」字符代碼而停頓。 (從電傳打字的日子來看,有更多的相關性)。在任何情況下,根本沒有代碼,沒有數據包捕獲來檢查,甚至沒有一個URL來嘗試自己,這是不可能的。你正在看一些來自某些東西的二進制數據。 – Brad

+0

好吧我現在更新了更多的信息 - 對不起之前沒有足夠的 – garson

+0

@Brad我已經添加了我的完整代碼 - 請讓我知道如果這仍然沒有寫到標準,我會關閉並重寫問題。謝謝 – garson

回答

0

我想通了!這個亂碼是由於我缺少'gzip:true'選項而引起的(我的請求)。現在第二個請求的內容如下:

url: urlstring2 
    ,gzip: true 
    ,headers:{... 
相關問題