javascript - 使用 NodeJS TLS 套接字的 HTTPS 通信

标签 javascript node.js sockets http https

我想从 NodeJS 与 Web 服务器通信。这是我尝试过的:

let tls = require("tls");
(async function() {
    let socket = tls.connect(443, "google.com", {servername: "google.com"});
    socket.on("OCSPResponse", function(response) {
        console.log(response);
    });
    socket.write(`GET / HTTP/1.1\r\nHost: google.com`);
})();
没有输出。我期望使用 net 对 HTTP 服务器使用类似的技术。模块

最佳答案

let tls = require("tls");
(async function() {
    let socket = tls.connect(443, "google.com", {servername: "google.com"});
    socket.on("data", function(response) {
        console.log(response);
    });
    socket.setEncoding("utf8");
    socket.write(`GET / HTTP/1.1\nHost: www.google.com\n\n`);
})();

关于javascript - 使用 NodeJS TLS 套接字的 HTTPS 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62911112/

相关文章:

javascript - 带 socket.io 的 Node.js : TypeError: Obj has no method 'on' at new Manager

javascript - 将插件应用到 DOM 中的新元素 (jquery)

javascript - 图片未出现在 Firefox 上

javascript - 为什么此代码块会产生无限循环?

javascript - 类型错误 : Cannot read property 'body' of undefined after POST (MEAN)

Linux 上的 PHP 套接字错误权限被拒绝

javascript - 如何在nodejs中使用数据和 header 重定向到另一个Web服务

node.js - 如何将 api key 传递给 Google Cloud Vision NodeJS API

java - 通过服务器线程调用OSGi服务

c - 在 IOCP 中发送数据的最佳方法是什么?