简体   繁体   中英

how to create a HTTP-1.0 request by net.socket or http.request?

I want to create a HTTP-1.0 request by net.socket or http.request but I have a problem with this code:

var http = require('http');

var options = {
   host:'google.com',
   method: 'CONNECT',
   path: 'google.com:80',
   port: 80
};

var req = http.request(options);
req.end();

req.on('connect', function(res, socket, head) {
   socket.write('GET / HTTP/1.0\r\n' +
                'Host: google.com:80\r\n' +
                '\r\n');
socket.on('data', function(chunk) {
    console.log(chunk);
});
socket.end();
});

I get same error:

events.js:66
        throw arguments[1]; // Unhandled 'error' event
                   ^
Error: Parse Error
    at Socket.socketOnData (http.js:1366:r20)
    at TCP.onread (net.js:403:27)

Your help is welcome.

http.request always uses HTTP/1.1, it's hardcoded in the source.

Here is the code to send HTTP/1.0 request using plain TCP stream (via net.connect ):

var net = require('net');

var opts = {
  host: 'google.com',
  port: 80
}

var socket = net.connect(opts, function() {
  socket.end('GET / HTTP/1.0\r\n' +
             'Host: google.com\r\n' +
              '\r\n');
});

socket.on('data', function(chunk) {
  console.log(chunk.toString());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM