2
我使用Node.js的,但我有一種感覺,這是沒有必然的聯繫,只是節點 - 反正在服務器端的HTTP請求重定向無限
我正在寫在節點的URL縮短,我想要達到縮短的網址,以獲得頁面標題 - 這在大多數情況下,通常遵循重定向,等等。
但是,當我打gmail.com,它進入無限重定向循環 -重定向到https://www.google.com/accounds/ServiceLogin?service=mail&passive=true&rm=false&continue=。 ......這又轉向自己永遠。
我的代碼基本上是一樣
var http = require('http'),
https = require('https'),
URL = require('url'),
querystring = require('url');
var http_client = {};
function _makeRequest(url, callback) {
var urlInfo = URL.parse(url);
var reqUrl = urlInfo.pathname || '/';
reqUrl += urlInfo.search || '';
reqUrl += urlInfo.hash || '';
var opts = {
host: urlInfo.hostname,
port: urlInfo.port || (urlInfo.protocol == 'https' ? 443 : 80),
path = reqUrl,
method: 'GET'
};
var protocol = (urlInfo.protocol == 'https' ? https : http);
var req = protocol.request(opts, function(res) {
var content = '';
res.setEncoding('utf8');
res.addListener('data', function(chunk) {
content += chunk;
});
res.addListener('end', function() {
_requestReceived(content, res.headers, callback);
});
});
req.end();
};
function _requestReceived(content, headers, callback) {
var redirect = false;
if(headers.location) {
newLocation = headers.location
redirect = true;
}
if(redirect) {
console.log('redirecting to :'+newLocation);
_makeRequest(newLocation, callback)
} else {
callback(null, content);
}
};
沒錯!
你的問題是什麼? – dhofstet 2011-03-04 05:54:40
如何阻止它無限重定向 - gmail不斷向我發送重定向標頭 – Lowgain 2011-03-05 21:25:35