Node.js https.request and https.get

本文详细介绍了如何使用Node.js中的https模块进行代理请求,包括TLS连接参数配置及实例应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    关于使用https客户端代理请求:

/**
 * Created by Administrator on 14-5-7.
 */

var https  = require('https');
    /**
     * 说明:下列指定了tls.connect()的参数配置说明,然而, globalAgent会忽略掉这些
     * pfx: 证书, 用于SSL的私秘钥证书文件. 默认 null.
    * key: 为SSL提供秘钥. 默认 null.
    * passphrase: 为 私秘钥 或者 pfx提供通行证字符串. 默认 null.
    * cert: 使用公共 x509证书. 默认 null.
    * ca: 权威证书或数组的权威证书检查远程主机.
    * ciphers: A string describing the ciphers to use or exclude. Consult http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT for details on the format.
    * rejectUnauthorized: If true, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent. Default true.
    * secureProtocol: The SSL method to use, e.g. SSLv3_method to force SSL version 3. The possible values depend on your installation of OpenSSL and are defined in the constant SSL_METHODS.
    */
    var options = {
        hostname: 'encrypted.google.com',
        port: 443,
        path: '/',
        method: 'GET',
        key: fs.readFileSync('certs/agent2-key.pem'),
        cert: fs.readFileSync('certs/agent2-cert.pem'),
        agent: false
    };

options.agent = new https.Agent(options);
var req = https.request(options ,function(res){
        //do something
    });
req.end();


https.get:

/**
 * Created by Antianlu on 14-5-7.
 */

var https = require('https');

/**
 * https.get的第一个参数如果为字符串会自动url.parse()转换为options的对象形式
 */
https.get('https://encrypted.google.com/', function(res) {
    console.log("statusCode: ", res.statusCode);
    console.log("headers: ", res.headers);

    res.on('data', function(d) {
        process.stdout.write(d);
    });

}).on('error', function(e) {
        console.error(e);
    });


转载于:https://my.oschina.net/antianlu/blog/261741

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值