NodeJS:利用 Axios 实现 HTTP、HTTPS 和 SOCKS5 代理请求

在日常开发中,网络请求是不可避免的。通过使用代理服务器,可以更好地控制请求的来源、隐藏 IP 地址,或者绕过网络限制。在本篇文章中,我将分享如何使用 axios 库结合 HTTP、HTTPS 和 SOCKS5 代理来发送网络请求,并详细介绍实现过程。

准备工作

首先,我们需要确保项目中安装了必要的依赖,包括 axioshttp-proxy-agenthttps-proxy-agentsocks-proxy-agent。可以使用以下命令进行安装:

bash 复制代码
npm install axios http-proxy-agent https-proxy-agent socks-proxy-agent

安装完成后,就可以开始构建代码了。

代码实现

1. 导入必要的模块

我们首先导入 axios 库及相应的代理模块:

js 复制代码
import axios from "axios";
import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";
import { SocksProxyAgent } from "socks-proxy-agent";

这里我们使用 axios 作为 HTTP 客户端,用它来发送网络请求;而 http-proxy-agenthttps-proxy-agentsocks-proxy-agent 用于处理不同类型的代理协议。

2. 配置 HTTP 代理

我们通过 HttpProxyAgent 配置 HTTP 代理:

js 复制代码
const http = () => {
  const httpAgent = new HttpProxyAgent("http://127.0.0.1:7899");
  httpAxios.defaults.httpAgent = httpAgent;
  httpAxios.defaults.proxy = true;

  httpAxios.get("http://ipinfo.io", {}).then((res) => {
    console.log(res.data);
  });
};
  • HttpProxyAgent("<http://127.0.0.1:7899>"):定义 HTTP 代理的地址。这里我们指定代理服务器位于本地的 7899 端口。
  • httpAxios.defaults.httpAgent:设置 axios 请求的 httpAgent,用于处理代理请求。
  • httpAxios.defaults.proxy = true:启用代理模式。
  • httpAxios.get("<http://ipinfo.io>"):发送 HTTP 请求,并输出响应内容。

3. 配置 HTTPS 代理

对于 HTTPS 请求,我们可以使用 HttpsProxyAgent

js 复制代码
const https = () => {
  const httpsAgent = new HttpsProxyAgent("http://127.0.0.1:7899");
  httpsAxios.defaults.httpsAgent = httpsAgent;
  httpsAxios.defaults.proxy = false;

  httpsAxios.get("https://ipinfo.io", {}).then((res) => {
    console.log(res.data);
  });
};
  • HttpsProxyAgent("<http://127.0.0.1:7899>"):这里我们依然使用同一个代理地址。
  • httpsAxios.defaults.httpsAgent:为 HTTPS 请求设置 httpsAgent
  • httpsAxios.defaults.proxy = false:禁用内置的 axios 代理,因为我们手动定义了代理。

4. 配置 SOCKS5 代理

为了支持 SOCKS5 代理,我们需要使用 SocksProxyAgent

js 复制代码
const socks5 = () => {
  const socks5Agent = new SocksProxyAgent("socks5://127.0.0.1:7898");
  socks5Axios.defaults.httpsAgent = socks5Agent;
  socks5Axios.defaults.proxy = false;

  socks5Axios.get("https://ipinfo.io").then((res) => {
    console.log(res.data);
  }).catch((err) => {
    console.log(err.message);
  });
};
  • SocksProxyAgent("socks5://127.0.0.1:7898"):定义 SOCKS5 代理地址。我们使用本地的 7898 端口。
  • socks5Axios.defaults.httpsAgent:将 socks5Agent 作为 axioshttpsAgent
  • socks5Axios.defaults.proxy = false:禁用 axios 自带的代理功能。

5. 执行代码

最后,我们将所有代理请求依次执行:

js 复制代码
http();
https();
socks5();

完整代码

以下是完整的代码实现:

js 复制代码
import axios from "axios";
import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";
import { SocksProxyAgent } from "socks-proxy-agent";

let httpAxios = axios;
let httpsAxios = axios;
let socks5Axios = axios;

const http = () => {
  const httpAgent = new HttpProxyAgent("http://127.0.0.1:7899");
  httpAxios.defaults.httpAgent = httpAgent;
  httpAxios.defaults.proxy = true;

  httpAxios.get("http://ipinfo.io", {}).then((res) => {
    console.log(res.data);
  });
};

const https = () => {
  const httpsAgent = new HttpsProxyAgent("http://127.0.0.1:7899");
  httpsAxios.defaults.httpsAgent = httpsAgent;
  httpsAxios.defaults.proxy = false;

  httpsAxios.get("https://ipinfo.io", {}).then((res) => {
    console.log(res.data);
  });
};

const socks5 = () => {
  const socks5Agent = new SocksProxyAgent("socks5://127.0.0.1:7898");
  socks5Axios.defaults.httpsAgent = socks5Agent;
  socks5Axios.defaults.proxy = false;

  socks5Axios.get("https://ipinfo.io").then((res) => {
    console.log(res.data);
  }).catch((err) => {
    console.log(err.message);
  });
};

http();
https();
socks5();
相关推荐
一只小姜丝3321 小时前
解决各大浏览器中http地址无权限调用麦克风摄像头问题
网络·vue.js·网络协议·http
hgdlip9 小时前
如果更换ip地址会怎么样?网络ip地址怎么更换
网络·网络协议·tcp/ip
m0_748252389 小时前
Node.js HTTP模块详解:创建服务器、响应请求与客户端请求
服务器·http·node.js
m0_7482365811 小时前
Nginx中$http_host、$host、$proxy_host的区别
运维·nginx·http
刘大猫2612 小时前
九、MyBatis动态SQL
人工智能·神经网络·网络协议
Ljw...12 小时前
HTTP
linux·网络·网络协议·http
追风赶月、13 小时前
【网络】HTTPS协议原理
网络·网络协议·https
桃酥40315 小时前
8、HTTP/1.0和HTTP/1.1的区别【高频】
网络·http
xing.yu.CTF17 小时前
4.3MISC流量分析练习-wireshark-https
测试工具·https·wireshark