通过代理请求url的Nodejs问题

通过代理请求url的Nodejs问题

var http = require(‘http’); var opt ={ host:‘119.184.214.18’, port:8080, method:‘GET’, path:‘http://42.96.192.46/echo_ip’, headers:{ } } var body = ‘’; var req=http.request(opt,function(res){ console.log("Got response: " + res.statusCode); res.on(‘data’, function(d){ body += d;
}).on(‘end’,function(){ console.log(res.header); console.log(body);
}); });

req.on(‘error’,function(e){ console.log(“Got error;” + e.message); });

req.end(); 为什么会报Parse Error??


4 回复

通过代理请求URL的Node.js问题

在你的代码中,你尝试通过一个HTTP代理来请求一个URL。然而,你在设置代理时并没有正确地配置代理信息,这导致了Parse Error。在Node.js中,使用代理进行HTTP请求通常需要额外的库来处理代理设置。

示例代码

你可以使用http-proxy-agenthttps-proxy-agent库来处理代理请求。以下是一个完整的示例代码:

const http = require('http');
const HttpsProxyAgent = require('https-proxy-agent');

// 定义代理服务器信息
const proxyHost = '119.184.214.18';
const proxyPort = 8080;

// 定义目标URL
const targetUrl = 'http://42.96.192.46/echo_ip';

// 创建代理服务器
const proxyAgent = new HttpsProxyAgent(`http://${proxyHost}:${proxyPort}`);

// 请求选项
const options = {
    agent: proxyAgent, // 使用代理
    hostname: '42.96.192.46',
    port: 80,
    path: '/echo_ip',
    method: 'GET'
};

let body = '';

const req = http.request(options, (res) => {
    console.log('Got response: ' + res.statusCode);

    res.on('data', (d) => {
        body += d;
    }).on('end', () => {
        console.log(res.headers);
        console.log(body);
    });
});

req.on('error', (e) => {
    console.log('Got error: ' + e.message);
});

req.end();

解释

  1. 引入依赖

    • http: Node.js内置模块,用于发起HTTP请求。
    • https-proxy-agent: 用于创建代理服务器实例。
  2. 定义代理和目标URL

    • proxyHostproxyPort 是代理服务器的地址和端口。
    • targetUrl 是你要请求的目标URL。
  3. 创建代理服务器实例

    • 使用HttpsProxyAgent创建代理服务器实例,传入代理服务器的地址。
  4. 请求选项

    • agent: 使用代理服务器实例。
    • hostname, port, path, method: 目标URL的相关信息。
  5. 发起请求

    • 使用http.request发起请求,并处理响应数据。

通过这种方式,你可以正确地通过代理服务器请求目标URL,避免出现Parse Error


你的path给错了, path不是url怎么还包含host部分呢…

应该是通过代理服务器转发这个url才对吧,貌似是host不对

在你的代码中,http.request 方法默认不会使用代理。如果你想通过代理来请求 URL,可以使用 http(s).request 的代理功能或者借助第三方库如 proxy-agent 来实现。

以下是一个使用 proxy-agent 的示例:

首先安装 proxy-agent

npm install proxy-agent

然后你可以这样修改你的代码:

const http = require('http');
const { ProxyAgent } = require('proxy-agent');

const options = {
  hostname: '42.96.192.46',
  port: 80,
  path: '/echo_ip',
  method: 'GET',
  headers: {},
  agent: new ProxyAgent('http://119.184.214.18:8080')
};

const req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  let body = '';
  res.on('data', (chunk) => {
    body += chunk;
  });
  res.on('end', () => {
    console.log(res.headers);
    console.log(body);
  });
});

req.on('error', (e) => {
  console.error(`Problem with request: ${e.message}`);
});

req.end();

解释:

  • ProxyAgentproxy-agent 库提供了一个 ProxyAgent 类,它可以配置为使用指定的代理服务器。
  • agent:在 options 对象中设置 agent 属性,并传递一个 ProxyAgent 实例,该实例指定了代理服务器的地址和端口。
  • hostname 和 port:在 options 中直接使用 hostnameport 而不是 host,以避免混淆。

如果你不使用第三方库,可以通过 http.request 的内部机制手动处理代理,但通常情况下使用 proxy-agent 更方便且更可靠。

回到顶部