通过NodeJS中的SOCKS5代理执行http请求 [英] Doing http requests through a SOCKS5 proxy in NodeJS

查看:2577
本文介绍了通过NodeJS中的SOCKS5代理执行http请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划通过Tor在NodeJS中执行一系列HTTP请求。

Tor使用SOCKS5,所以我出去搜索了一种在NodeJS中代理HTTP请求的方法。

我打算使用默认的http.request()函数来完成工作。但是,我似乎找不到使用代理的方法。有人建议我可以这样做:

I'm planning to do a series of HTTP requests in NodeJS though Tor.
Tor uses SOCKS5 so I went out and searched for a way to proxify HTTP requests in NodeJS.
I'm planning to the the default http.request() function to do the work.
However, I can't seem to find a way to use a proxy with that.
Someone suggested that I could do this:

var http = require("http");
var options = {
  host: "localhost",
  port: 9050,
  path: "http://check.torproject.org",
  method: 'GET',
  headers: {
    Host: "http://check.torproject.org",
  }
};
var req = http.request(options, function(res) {
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

但它不起作用。

那么,有什么建议吗?

But it didn't work.
So, any suggestions?

推荐答案

我刚刚发布了两个可以帮助你做到这一点的模块: socks5- http -client socks5- https -client

I've just published two modules that should help you do this: socks5-http-client and socks5-https-client.

只需使用那些而不是默认 http 模块。 API是一样的。例如:

Just use those instead of the default http module. The API is the same. For example:

require('socks5-http-client').request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});

这篇关于通过NodeJS中的SOCKS5代理执行http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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