mm_https
v1.6.5
Published
超级美眉HTTP请求模块 - 支持自动Cookie管理、代理设置、文件下载等功能
Maintainers
Readme
mm_https
这是一个基于Node.js的HTTP请求帮助类模块,支持自动管理cookie、模拟登录、代理设置等功能。
安装
npm install mm_https基本用法
初始化
const {
Http
} = require('mm_https');
// 基本初始化
const http = new Http();
// 带配置初始化
const httpWithConfig = new Http({
headers: {
'User-Agent': 'Custom User Agent'
},
cookie: 'session=123456',
proxy: 'http://127.0.0.1:10809' // 设置代理
});配置选项
headers: 自定义请求头,可以是对象或字符串格式cookie: 初始cookie字符串proxy: 代理服务器地址rejectUnauthorized: SSL证书验证选项,默认false
核心功能
GET请求
const {
Http
} = new Http();
const http = new Http();
// 基本GET请求
async function simpleGet() {
const res = await http.get('https://api.example.com/data');
$.log.debug(res.body); // 响应内容
}
// 带请求头的GET请求
async function getWithHeaders() {
const headers = {
'Authorization': 'Bearer token123'
};
const res = await http.get('https://api.example.com/protected', headers);
$.log.debug(res.body);
}
// 带cookie的GET请求
async function getWithCookie() {
const res = await http.get('https://api.example.com/user', null, 'session=abc123');
$.log.debug(res.body);
}POST请求
const {
Http
} = new Http();
const http = new Http();
// JSON格式POST请求
async function postJson() {
const data = {
username: 'test',
password: '123456'
};
const res = await http.post('https://api.example.com/login', data);
$.log.debug(res.body);
}
// 表单格式POST请求
async function postForm() {
const formData = {
name: 'John',
age: 25
};
const res = await http.post('https://api.example.com/submit', formData, null, 'form');
$.log.debug(res.body);
}
// 自定义请求头和cookie的POST请求
async function postWithHeadersAndCookie() {
const data = { id: 123 };
const headers = { 'X-Custom-Header': 'value' };
const res = await http.post('https://api.example.com/api', data, headers, 'json', 'session=xyz789');
$.log.debug(res.body);
}文件下载
const {
Http
} = new Http();
const http = new Http();
// 基本文件下载
async function downloadFile() {
const url = 'https://example.com/image.jpg';
const filename = './downloads/image.jpg';
const file = await http.download(url, filename);
$.log.debug('下载文件保存在:', file);
}
// 自动识别文件类型下载
async function autoDownload() {
const url = 'https://example.com/image.jpg';
const filename = './downloads/image'; // 无需指定扩展名
const file = await http.download(url, filename, true);
$.log.debug('下载文件保存在:', file);
}
// 带请求头的下载
async function downloadWithHeaders() {
const url = 'https://example.com/protected/file.pdf';
const filename = './downloads/document.pdf';
const headers = {
'Authorization': 'Bearer token123'
};
const file = await http.download(url, filename, false, headers);
$.log.debug('下载文件保存在:', file);
}Cookie管理
const {
Http
} = new Http();
const http = new Http();
// 设置Cookie
http.setCookie('session=abc123; user=john');
// 或者通过对象设置多个Cookie
http.setCookie({
'session': 'abc123',
'user': 'john'
});
// 获取当前所有Cookie
const cookieString = http.getCookie();
$.log.debug('当前Cookie:', cookieString);
// 清除所有Cookie
http.clear();高级用法
代理设置
// 使用代理服务器
const http = new Http({
proxy: 'http://127.0.0.1:10809'
});
// 访问需要代理的网站
async function accessWithProxy() {
const res = await http.get('https://google.com');
$.log.debug(res.body);
}自动重定向处理
模块会自动处理301、302等重定向响应,无需额外配置。
自定义请求
// 使用req方法发起自定义请求
async function customRequest() {
const options = {
method: 'POST',
href: 'https://api.example.com/data',
body: { key: 'value' },
headers: { 'X-Custom': 'value' },
type: 'json',
cookie: 'session=123'
};
const res = await http.req(options);
$.log.debug(res.body);
}响应对象
所有请求方法返回的响应对象包含以下字段:
status: 状态码和消息headers: 响应头body: 响应内容(字符串)buffer: 原始响应数据(Buffer对象,适用于二进制数据)
错误处理
async function handleErrors() {
try {
const res = await http.get('https://api.example.com/notfound');
$.log.debug(res.body);
} catch (err) {
$.log.error('请求失败:', err.message);
}
}注意事项
- 所有请求方法都返回Promise,需要使用async/await或.then()处理
- 默认已配置常用的请求头,可以通过配置覆盖
- 支持自动解码gzip、deflate等压缩响应
- 支持GB2312和GBK编码的响应自动转换
许可证
ISC License
