如何在node.js https / request Client中使用https代理? [英] How can I use an https proxy with node.js https/request Client?

查看:157
本文介绍了如何在node.js https / request Client中使用https代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过Intranet代理将我的客户端HTTPS请求发送到服务器。
我同时使用https和request + global-tunnel,两种解决方案似乎都不起作用。

与'http'类似的代码有效。我错过了其他设置吗?

I need to send my client HTTPS requests through an intranet proxy to a server. I use both https and request+global-tunnel and neither solutions seem to work.
The similar code with 'http' works. Is there other settings I missed?

代码失败并显示错误:
REQUEST:
请求问题:无法建立隧道套接字, cause = socket挂断

The code failed with an error: REQUEST: problem with request: tunneling socket could not be established, cause=socket hang up

HTTPS:
events.js:72
throw er; //未处理的'错误'事件
^
错误:套接字在SecurePair.error挂起
(tls.js:1011:23)
在EncryptedStream.CryptoStream._done(tls .js:703:22)CleartextStream.read的
[as _read](tls.js:499:24)

HTTPS: events.js:72 throw er; // Unhandled 'error' event ^ Error: socket hang up at SecurePair.error (tls.js:1011:23) at EncryptedStream.CryptoStream._done (tls.js:703:22) at CleartextStream.read [as _read] (tls.js:499:24)


  • 代码是简单的https测试。

  • The code is the simple https test.

var http = require(https);

var http = require("https");

var options = {
host: proxy.myplace.com,
端口:912,
路径: https:// www。 google.com
标题:{
主持人:www.google.com
}
};

var options = { host: "proxy.myplace.com", port: 912, path: "https://www.google.com", headers: { Host: "www.google.com" } };

http.get(选项,函数(res){
console.log(res);
res.pipe(process.stdout);
});

http.get(options, function(res) { console.log(res); res.pipe(process.stdout); });

推荐答案

您可能希望在节点应用和目标目标之间建立TLS加密连接通过代理。

You probably want to establish a TLS encrypted connection between your node app and target destination through a proxy.

为此,您需要发送一个带有目标目标主机名和端口的CONNECT请求。代理将创建到目标主机的TCP连接,然后简单地在您和目标目标之间转发包。

In order to do this you need to send a CONNECT request with the target destination host name and port. The proxy will create a TCP connection to the target host and then simply forwards packs between you and the target destination.

我强烈建议使用请求客户端。该软件包简化了发出HTTP / S请求的过程和处理。

I highly recommend using the request client. This package simplifies the process and handling of making HTTP/S requests.

使用请求客户

var request = require('request');

request({
    url: 'https://www.google.com',
    proxy: 'http://97.77.104.22:3128'
}, function (error, response, body) {
    if (error) {
        console.log(error);
    } else {
        console.log(response);
    }
});

不使用外部依赖项的示例代码:

Example code using no external dependencies:

var http = require('http'),
    tls = require('tls');

var req = http.request({
    host: '97.77.104.22',
    port: 3128,
    method: 'CONNECT',
    path: 'twitter.com:443'
});

req.on('connect', function (res, socket, head) {
    var tlsConnection = tls.connect({
        host: 'twitter.com',
        socket: socket
    }, function () {
        tlsConnection.write('GET / HTTP/1.1\r\nHost: twitter.com\r\n\r\n');
    });

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

req.end();

这篇关于如何在node.js https / request Client中使用https代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆