通过套接字发送的节点JS https原始请求格式错误

时间:2019-04-10 19:17:25

标签: node.js sockets http https

我正在使用NodeJS中的https模块在连接操作后发送原始HTTP请求。 使用NodeJS文档中给出的示例,我从提琴手中提取了一个RAW请求并进行了发送,但是google发送回了Your client has issued a malformed or illegal request.

该请求应该可以正常工作,因为它是直接从Fiddler上的工作请求中获取的,并且使用带有相关标头的请求模块肯定可以工作。

const options = {
  hostname: "google.com",
  port: 443,
  method: "CONNECT"
};

const req = https.request(options);
req.end();

req.on("connect", (res, socket, head) => {
  console.log("Socket connected");
  socket.write(
    "GET https://www.google.com/ HTTP/1.1\r\n" +
      "Host: www.google.com\r\n" +
      "Connection: keep-alive\r\n" +
      "Upgrade-Insecure-Requests: 1\r\n" +
      "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36\r\n" +
      "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\r\n" +
      "Accept-Encoding: gzip, deflate, br\r\n" +
      "Accept-Language: en-US,en;q=0.9\r\n"
  );
  socket.on("data", chunk => {
    console.log(chunk.toString());
  });
  socket.on("end", () => {
    console.log("Socket was closed");
  });
});

我期望浏览器得到正常响应,但是我从谷歌那里得到了以下信息。

<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>400.</b> <ins>That’s an error.</ins>
  <p>Your client has issued a malformed or illegal request.  <ins>That’s all we know.</ins>

0 个答案:

没有答案