# mm_https **Repository Path**: qiuwenwu91/mm_https ## Basic Information - **Project Name**: mm_https - **Description**: No description available - **Primary Language**: NodeJS - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-02-17 - **Last Updated**: 2025-12-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mm_https 这是一个基于Node.js的HTTP请求帮助类模块,支持自动管理cookie、模拟登录、代理设置等功能。 ## 安装 ```bash npm install mm_https ``` ## 基本用法 ### 初始化 ```javascript 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请求 ```javascript 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请求 ```javascript 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); } ``` ### 文件下载 ```javascript 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管理 ```javascript 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(); ``` ## 高级用法 ### 代理设置 ```javascript // 使用代理服务器 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等重定向响应,无需额外配置。 ### 自定义请求 ```javascript // 使用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对象,适用于二进制数据) ## 错误处理 ```javascript async function handleErrors() { try { const res = await http.get('https://api.example.com/notfound'); $.log.debug(res.body); } catch (err) { $.log.error('请求失败:', err.message); } } ``` ## 注意事项 1. 所有请求方法都返回Promise,需要使用async/await或.then()处理 2. 默认已配置常用的请求头,可以通过配置覆盖 3. 支持自动解码gzip、deflate等压缩响应 4. 支持GB2312和GBK编码的响应自动转换 ## 许可证 ISC License