按照标题,我该怎么做?
这是我的代码:
var http = require('http');
// to access this url I need to put basic auth.
var client = http.createClient(80, 'www.example.com');
var request = client.request('GET', '/', {
'host': 'www.example.com'
});
request.end();
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
Run Code Online (Sandbox Code Playgroud) 我打算通过Tor在NodeJS中做一系列的HTTP请求.
Tor使用SOCKS5,所以我出去搜索了一种在NodeJS中代理HTTP请求的方法.
我打算使用默认的http.request()函数来完成工作.
但是,我似乎无法找到使用代理的方法.
有人建议我可以这样做:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
但它没有用.
那么,有什么建议吗?
我正在使用浏览器。以下代码由 webpack 编译。我试过这个:
const axios = require('axios');
var res = await axios.get('https://api.ipify.org?format=json', {
proxy: {
host: 'proxy-url',
port: 80,
auth: {username: 'my-user', password: 'my-password'}
}
});
console.log(res.data); // gives my ip and not the proxy's one.
Run Code Online (Sandbox Code Playgroud)
我也用相同的代码尝试过这个,但它不起作用:
const axios = require('axios-https-proxy-fix');
Run Code Online (Sandbox Code Playgroud)
然后,我尝试使用 httpsAgent:
const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent')
var agent = new HttpsProxyAgent('http://my-user:my-pass@proxy-url:port');
var res = await axios.get('https://api.ipify.org?format=json', {
httpsAgent: agent,
});
console.log(res.data); // gives my ip and not the proxy's one.
Run Code Online (Sandbox Code Playgroud)
这是一个错误吗?我是被诅咒了还是我在阅读文档时遇到了问题?
我用NodeJS训练myslef并尝试了一个简单的GET调用.这是我的代码:
var http = require('http');
var options = {
host: 'www.boardgamegeek.com',
path: '/xmlapi/boardgame/1?stats=1',
method: 'GET'
}
var request = http.request(options, function (response) {
var str = ""
response.on('data', function (data) {
str += data;
});
response.on('end', function () {
console.log(str);
});
});
request.on('error', function (e) {
console.log('Problem with request: ' + e.message);
});
request.end();
Run Code Online (Sandbox Code Playgroud)
调用的URL似乎可以在我的浏览器中运行https://www.boardgamegeek.com/xmlapi/boardgame/1?stats=1
无论如何,我遇到了请求问题:当我运行代码时连接ETIMEDOUT并且我不知道如何修复它.
什么可能导致此错误?这是我的代码还是网络/代理问题?
当我向Google API发送请求时(使用axios或仅使用https),例如 https://www.googleapis.com/blogger/v3/blogs/2399953?key=...
我总是会遇到“ 在建立安全TLS连接之前客户端网络套接字已断开连接 ”的错误。
但是,如果我将请求发送到https://api.github.com,它就可以正常工作。我已经搜索了错误,但是找不到很多有用的信息。在这里https://github.com/nodejs/node/issues/21088说,如果服务器使用TLS 1.0,则可能会发生,但显然不是我的情况。
我也尝试使用googleapis,但仍然遇到相同的错误。
任何想法如何解决该错误?
我试图用协议分析器(Charles)捕获node.js http流量但是无法让节点使用代理.有没有办法让节点http和https模块使用代理?
我顺便使用OSX
我被 Socket.io 交易所困住了。如果 javascript 代码托管在浏览器 (Chrome/Firefox) 内,则无论中间有或没有代理,连接都可以正常工作。
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script>
var socket = io('https://uri', { secure: true, reconnect: true, rejectUnauthorized: false });
socket.on('connect', function () {
console.log('Connected to server!');
socket.emit('register', 'ClientName');
});
</script>
Run Code Online (Sandbox Code Playgroud)
相反,相同的代码在 Nodejs 上运行,使用 Node v10.4.0 和模块“socket.io”:“^2.1.1”仅在直接连接时才有效。
我尝试使用socket.io-proxy(相当旧),但似乎它与socket.io-client 不一致,并且它不起作用,或者我遗漏了一些东西。
很明显,“浏览器脚本”可以访问代理设置/通道......无论如何,或者节点运行时不知道的某些其他设置。
感谢您的任何建议。
洛伦佐