【问题标题】:node js request proxy节点js请求代理
【发布时间】:2018-01-02 16:41:58
【问题描述】:

我通过代理发送请求并且总是收到这样的响应

tunneling socket could not be established, cause=read ECONNRESET

tunneling socket could not be established, cause= socket hang up

我的代码

      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
})

我做错了什么?

现在出现此错误:错误:隧道创建失败。套接字错误:错误:读取 ECONNRESET

我的代码:

  const request = require('request'),
  proxyingAgent = require('proxying-agent');

;

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://${obj.proxy[obj.proxyIdx]}`,
    agent: proxyingAgent.create(`http://${obj.proxy[obj.proxyIdx]}`, url),
  }

【问题讨论】:

    标签: node.js proxy request


    【解决方案1】:

    关于您的代码,问题可能在于您的settings 对象。
    您需要使用如下语法:

    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 包proxying agent 建立代理连接。
    您的代码应如下所示:

    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)
      };
    };
    

    您可以将它与您的 url 和凭据一起使用。我们将使用fetch 包。

    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}`);
        }
      };
    

    【讨论】:

    • @Inomezi 我猜问题在于对您的代理的身份验证。 ECONNRESET 错误表示连接错误。 ECONNRESET 意味着 TCP 对话的另一端突然关闭了它的连接端。这很可能是由于一个或多个应用程序协议错误造成的。因此,我的建议是检查您是否需要一些凭据(例如用户名/密码)来访问您的代理。
    猜你喜欢
    • 2023-02-21
    • 2017-08-19
    • 2012-04-27
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    相关资源
    最近更新 更多