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();
相关推荐
iOS技术狂热者2 小时前
多图超详细安装flutter&Android Studio开发环境,并配置插件
websocket·网络协议·tcp/ip·http·网络安全·https·udp
今夜有雨.2 小时前
HTTP---基础知识
服务器·网络·后端·网络协议·学习·tcp/ip·http
盛满暮色 风止何安5 小时前
VLAN的高级特性
运维·服务器·开发语言·网络·网络协议·网络安全·php
yangpipi-5 小时前
6. 王道_网络协议
网络·网络协议
techdashen8 小时前
性能比拼: TCP vs UDP(第三轮)
网络协议·tcp/ip·udp
无名之逆8 小时前
hyperlane:Rust HTTP 服务器开发的不二之选
服务器·开发语言·前端·后端·安全·http·rust
独家回忆3649 小时前
网络原理-TCP/IP
网络·网络协议·tcp/ip
残诗11 小时前
ar头显和眼镜图像特效处理
网络·网络协议
。puppy11 小时前
HCIA—— 31 HTTP的报文、请求响应报文、方法、URI和URL
网络·网络协议·http
无职转生真好看12 小时前
HTTP和HTTPS区别
网络协议·http·https