我有一个代理“ net”连接,我需要通过与代理服务器的一个稳定连接将请求发送到一些不同的域和协议(http,https,ftp等)。
var socket = net.connect(
1111, // proxy port
'xx.xx.xxx.xx' // proxy host
);
socket.on('connect', function(a) {
console.log('--- connect', socket.remoteAddress);
// connecting to https host
socket.write(
'CONNECT www.google.com:443 HTTP/1.1\r\n' +
'Proxy-Connections: keep-alive\r\n' +
'Host: www.google.com\r\n\r\n',
function() {
// send GET request and wait for response
socket.write(
'GET / HTTP/1.0\r\n' +
'User-Agent: Node.js/0.6.6\r\n' +
'Connection: keep-alive\r\n' +
'Host: www.google.com\r\n\r\n',
function(a) {
return socket;
}
);
/*
another requests to different protocols/ports...
socket.write(...)
*/
return socket;
});
});
socket.on('data', function(a) {
console.log('--- data', a.toString());
});
socket.on('drain', function() {
console.log('drain!');
});
socket.on('timeout', function() {
console.log('timeout!');
});
socket.on('close', function() {
console.log('Connection closed');
});
socket.on('end', function() {
console.log('Connection end');
});
执行后,我得到下一个结果:
--- connect xx.xx.xxx.xx
--- data HTTP/1.0 200 Connection established
Connection end
Connection closed
只有一个响应为“连接已建立”,但我无法从“ GET”请求中获得响应。收到响应后,连接将关闭。
我错了哪里?用Google搜索它,但是发现只有一个解决方案-为http或其他协议以及https创建不同的连接,但是我只需要从一个稳定的连接发送请求。