Node.js 节点js请求代理
我通过代理发送请求,并且总是收到这样的响应Node.js 节点js请求代理,node.js,proxy,request,Node.js,Proxy,Request,我通过代理发送请求,并且总是收到这样的响应 tunneling socket could not be established, cause=read ECONNRESET 或 我的代码 let settings = { url: `url`, headers: { 'Connection': 'keep-alive', 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Appl
tunneling socket could not be established, cause=read ECONNRESET
或
我的代码
let settings = {
url: `url`,
headers: {
'Connection': 'keep-alive',
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
},
method: 'POST',
proxy: `http://${ip}:${port}`,
strictSSL: false
}
request.request(settings, (err, response, body) => {
// err here
})
我做错了什么
现在出现以下错误:错误:隧道创建失败。套接字错误:错误:读取EconReset
我的代码:
const request = require('request'),
proxyingAgent = require('proxying-agent');
)
关于代码,问题可能在于
设置
对象。
您需要使用如下语法:
let settings = {
url,
headers: {
'Connection': 'keep-alive',
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
},
method: 'POST',
proxy: `http://${ip}:${port}`,
strictSSL: false
}
在这里,我们使用ES6来缩短对象。但是,您也可以与npm包建立代理连接。
您的代码应该如下所示:
const proxyingAgent = require('proxying-agent');
const fetch = require('node-fetch');
const host = <your host>;
const port = <port>;
const creds = {
login: 'username',
password: 'pass'
};
const port = <proxy port>;
const buildProxy = (url) => {
return {
agent: proxyingAgent.create(`http://${creds.login}:${creds.password}@${host}:${port}`, url)
};
};
//If you don't have credentials for proxy, you can rewrite function
const buildProxyWithoutCreds = (url) => {
return {
agent: proxyingAgent.create(`http://${host}:${port}`, url)
};
};
@我想问题在于对代理的身份验证。EconReset错误表示连接错误。EconReset表示TCP会话的另一端突然关闭了其连接端。这很可能是由于一个或多个应用程序协议错误造成的。因此,我建议您检查是否需要用户名/密码等凭据来访问代理。
let settings = {
url,
headers: {
'Connection': 'keep-alive',
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
},
method: 'POST',
proxy: `http://${ip}:${port}`,
strictSSL: false
}
const proxyingAgent = require('proxying-agent');
const fetch = require('node-fetch');
const host = <your host>;
const port = <port>;
const creds = {
login: 'username',
password: 'pass'
};
const port = <proxy port>;
const buildProxy = (url) => {
return {
agent: proxyingAgent.create(`http://${creds.login}:${creds.password}@${host}:${port}`, url)
};
};
//If you don't have credentials for proxy, you can rewrite function
const buildProxyWithoutCreds = (url) => {
return {
agent: proxyingAgent.create(`http://${host}:${port}`, url)
};
};
const proxyGetData = async (url, type) => {
try {
const proxyData = buildProxyWithoutCreds(url);
// Make request with proxy. Here we use promise based library node-fetch
let req = await fetch(url, proxyData);
if (req.status === 200) {
return await req[type]();
}
return false;
} catch (e) {
throw new Error(`Error during request: ${e.message}`);
}
};