Created
August 5, 2012 23:24
-
-
Save tony1223/3267929 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 http = require('http'), | |
| Iconv = require('iconv').Iconv; | |
| function wget (host, path, callback,encoding /* optional */) { | |
| var iconv = null; | |
| if (encoding){ | |
| iconv = new Iconv(encoding,'UTF-8//TRANSLIT//IGNORE'); | |
| } | |
| var options = { | |
| host: host, | |
| port: 80, | |
| path: path, | |
| method: 'GET', | |
| }; | |
| var req = http.request(options, function(res) { | |
| //console.log('STATUS: ' + res.statusCode); | |
| //console.log('HEADERS: ' + JSON.stringify(res.headers)); | |
| var result = []; | |
| res.on('data', function (chunk) { | |
| if (iconv){ | |
| result.push(iconv.convert(new Buffer(chunk))); | |
| }else{ | |
| result.push(chunk); | |
| } | |
| }); | |
| res.on('end', function (){ | |
| callback(result.join("")); | |
| }); | |
| }); | |
| req.setTimeout(2000); | |
| req.on('error', function(e) { | |
| console.log('problem with request: ' + e.message); | |
| }); | |
| req.end(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment