Created
August 4, 2014 07:01
-
-
Save vymarkov/1d1b3b7e0b18ed5389e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var zlib = require('zlib'); | |
var http = require('http'); | |
var headers = { | |
"accept-charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3", | |
"accept-language": "en-US,en;q=0.8", | |
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", | |
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2", | |
"accept-encoding": "gzip,deflate", | |
}; | |
var options = { | |
hostname: "www.google.com", | |
// port: 80, | |
headers: headers | |
}; | |
var req = http.request(options, function(res) { | |
var body = ""; | |
res.on('error', function(err) { | |
console.log(err); | |
}); | |
var output; | |
if (res.headers['content-encoding'] == 'gzip') { | |
var gzip = zlib.createGunzip(); | |
res.pipe(gzip); | |
output = gzip; | |
} else { | |
output = res; | |
} | |
output.on('data', function(data) { | |
data = data.toString('utf-8'); | |
body += data; | |
}); | |
output.on('end', function() { | |
console.log(body); | |
}); | |
}); | |
req.on('error', function(err) { | |
console.log(err); | |
}) | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment