node.js - nodejs http.get如何使用proxy代理

查看:897
本文介绍了node.js - nodejs http.get如何使用proxy代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在使用request模块的时候

var targetOptions = {
        method: 'GET',
        url: 'http://url',
        timeout: 8000,
        encoding: null,
    };
targetOptions.proxy = 'http://213.183.252.156:8081'; //代理服务器
request(targetOptions, function (error, req,body) {
})

可以使用代理进行HTTP请求

在HTTP模块中 http.get
是否也可以达到如上的效果?

解决方案

可以,参考代码

var http = require('http');

var options = {
    hostname : '213.183.252.156',
    port     : 8081,
    path     : 'imququ.com:80',
    method     : 'CONNECT'
};

var req = http.request(options);

req.on('connect', function(res, socket) {
    socket.write('GET / HTTP/1.1\r\n' +
                 'Host: imququ.com\r\n' +
                 'Connection: Close\r\n' +
                 '\r\n');

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

    socket.on('end', function() {
        console.log('socket end.');
    });
});

req.end();

详细请看imququ 关于 http 代理的相关知识总结,讲得很好
https://imququ.com/post/web-p...

这篇关于node.js - nodejs http.get如何使用proxy代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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