mm_https 1.6.1 → 1.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +221 -0
  2. package/index.js +13 -53
  3. package/package.json +37 -37
  4. package/test.js +2 -1
package/README.md ADDED
@@ -0,0 +1,221 @@
1
+ # mm_https
2
+
3
+ 这是一个基于Node.js的HTTP请求帮助类模块,支持自动管理cookie、模拟登录、代理设置等功能。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install mm_https
9
+ ```
10
+
11
+ ## 基本用法
12
+
13
+ ### 初始化
14
+
15
+ ```javascript
16
+ const Http = require('mm_https');
17
+
18
+ // 基本初始化
19
+ const http = new Http();
20
+
21
+ // 带配置初始化
22
+ const httpWithConfig = new Http({
23
+ headers: {
24
+ 'User-Agent': 'Custom User Agent'
25
+ },
26
+ cookie: 'session=123456',
27
+ proxy: 'http://127.0.0.1:10809' // 设置代理
28
+ });
29
+ ```
30
+
31
+ ### 配置选项
32
+
33
+ - `headers`: 自定义请求头,可以是对象或字符串格式
34
+ - `cookie`: 初始cookie字符串
35
+ - `proxy`: 代理服务器地址
36
+ - `rejectUnauthorized`: SSL证书验证选项,默认false
37
+
38
+ ## 核心功能
39
+
40
+ ### GET请求
41
+
42
+ ```javascript
43
+ const http = new Http();
44
+
45
+ // 基本GET请求
46
+ async function simpleGet() {
47
+ const res = await http.get('https://api.example.com/data');
48
+ console.log(res.body); // 响应内容
49
+ }
50
+
51
+ // 带请求头的GET请求
52
+ async function getWithHeaders() {
53
+ const headers = {
54
+ 'Authorization': 'Bearer token123'
55
+ };
56
+ const res = await http.get('https://api.example.com/protected', headers);
57
+ console.log(res.body);
58
+ }
59
+
60
+ // 带cookie的GET请求
61
+ async function getWithCookie() {
62
+ const res = await http.get('https://api.example.com/user', null, 'session=abc123');
63
+ console.log(res.body);
64
+ }
65
+ ```
66
+
67
+ ### POST请求
68
+
69
+ ```javascript
70
+ const http = new Http();
71
+
72
+ // JSON格式POST请求
73
+ async function postJson() {
74
+ const data = {
75
+ username: 'test',
76
+ password: '123456'
77
+ };
78
+ const res = await http.post('https://api.example.com/login', data);
79
+ console.log(res.body);
80
+ }
81
+
82
+ // 表单格式POST请求
83
+ async function postForm() {
84
+ const formData = {
85
+ name: 'John',
86
+ age: 25
87
+ };
88
+ const res = await http.post('https://api.example.com/submit', formData, null, 'form');
89
+ console.log(res.body);
90
+ }
91
+
92
+ // 自定义请求头和cookie的POST请求
93
+ async function postWithHeadersAndCookie() {
94
+ const data = { id: 123 };
95
+ const headers = { 'X-Custom-Header': 'value' };
96
+ const res = await http.post('https://api.example.com/api', data, headers, 'json', 'session=xyz789');
97
+ console.log(res.body);
98
+ }
99
+ ```
100
+
101
+ ### 文件下载
102
+
103
+ ```javascript
104
+ const http = new Http();
105
+
106
+ // 基本文件下载
107
+ async function downloadFile() {
108
+ const url = 'https://example.com/image.jpg';
109
+ const filename = './downloads/image.jpg';
110
+ const file = await http.download(url, filename);
111
+ console.log('下载文件保存在:', file);
112
+ }
113
+
114
+ // 自动识别文件类型下载
115
+ async function autoDownload() {
116
+ const url = 'https://example.com/image.jpg';
117
+ const filename = './downloads/image'; // 无需指定扩展名
118
+ const file = await http.download(url, filename, true);
119
+ console.log('下载文件保存在:', file);
120
+ }
121
+
122
+ // 带请求头的下载
123
+ async function downloadWithHeaders() {
124
+ const url = 'https://example.com/protected/file.pdf';
125
+ const filename = './downloads/document.pdf';
126
+ const headers = {
127
+ 'Authorization': 'Bearer token123'
128
+ };
129
+ const file = await http.download(url, filename, false, headers);
130
+ console.log('下载文件保存在:', file);
131
+ }
132
+ ```
133
+
134
+ ### Cookie管理
135
+
136
+ ```javascript
137
+ const http = new Http();
138
+
139
+ // 设置Cookie
140
+ http.set_cookie('session=abc123; user=john');
141
+
142
+ // 获取当前所有Cookie
143
+ const cookieString = http.get_cookie();
144
+ console.log('当前Cookie:', cookieString);
145
+
146
+ // 清除所有Cookie
147
+ http.clear();
148
+ ```
149
+
150
+ ## 高级用法
151
+
152
+ ### 代理设置
153
+
154
+ ```javascript
155
+ // 使用代理服务器
156
+ const http = new Http({
157
+ proxy: 'http://127.0.0.1:10809'
158
+ });
159
+
160
+ // 访问需要代理的网站
161
+ async function accessWithProxy() {
162
+ const res = await http.get('https://google.com');
163
+ console.log(res.body);
164
+ }
165
+ ```
166
+
167
+ ### 自动重定向处理
168
+
169
+ 模块会自动处理301、302等重定向响应,无需额外配置。
170
+
171
+ ### 自定义请求
172
+
173
+ ```javascript
174
+ // 使用req方法发起自定义请求
175
+ async function customRequest() {
176
+ const options = {
177
+ method: 'POST',
178
+ href: 'https://api.example.com/data',
179
+ body: { key: 'value' },
180
+ headers: { 'X-Custom': 'value' },
181
+ type: 'json',
182
+ cookie: 'session=123'
183
+ };
184
+
185
+ const res = await http.req(options);
186
+ console.log(res.body);
187
+ }
188
+ ```
189
+
190
+ ## 响应对象
191
+
192
+ 所有请求方法返回的响应对象包含以下字段:
193
+
194
+ - `status`: 状态码和消息
195
+ - `headers`: 响应头
196
+ - `body`: 响应内容(字符串)
197
+ - `buffer`: 原始响应数据(Buffer对象,适用于二进制数据)
198
+
199
+ ## 错误处理
200
+
201
+ ```javascript
202
+ async function handleErrors() {
203
+ try {
204
+ const res = await http.get('https://api.example.com/notfound');
205
+ console.log(res.body);
206
+ } catch (err) {
207
+ console.error('请求失败:', err.message);
208
+ }
209
+ }
210
+ ```
211
+
212
+ ## 注意事项
213
+
214
+ 1. 所有请求方法都返回Promise,需要使用async/await或.then()处理
215
+ 2. 默认已配置常用的请求头,可以通过配置覆盖
216
+ 3. 支持自动解码gzip、deflate等压缩响应
217
+ 4. 支持GB2312和GBK编码的响应自动转换
218
+
219
+ ## 许可证
220
+
221
+ ISC License
package/index.js CHANGED
@@ -44,13 +44,13 @@ class Http {
44
44
  'Accept-Language': 'zh-CN,zh;q=0.9',
45
45
  'Cache-Control': 'no-cache',
46
46
  'Connection': 'keep-alive',
47
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
47
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
48
48
  };
49
49
 
50
50
  if (this.config.cookie) {
51
51
  this.set_cookie(this.config.cookie);
52
52
  }
53
-
53
+
54
54
  if (this.config.headers) {
55
55
  var headers = this.change_headers(config.headers);
56
56
  Object.assign(this.headers, headers);
@@ -144,46 +144,6 @@ Http.prototype.http_options = function(options) {
144
144
  }, u);
145
145
  }
146
146
 
147
- // Http.prototype.http_options = function(options) {
148
- // var cg = this.config;
149
- // var {
150
- // href,
151
- // body,
152
- // headers,
153
- // method,
154
- // cookie,
155
- // type
156
- // } = options;
157
- // var protocol = href.left(":/") || 'http';
158
- // // 移除协议头 取端口号
159
- // var u = href.replace(protocol + "://", "");
160
- // var port = u.between(":", "/") || u.left("/", true).right(":") || (protocol === 'http' ? 80 : 443);
161
-
162
- // // 移除端口号,取主机地址
163
- // u = u.replace(":" + port, "");
164
- // var hostname = u.left("/", true).left("?", true);
165
- // // 移除主机地址,取路由路径
166
- // u = u.replace(hostname, "");
167
- // var path = u.left("?", true);
168
- // if (cookie) {
169
- // this.set_cookie(cookie);
170
- // }
171
- // return {
172
- // href,
173
- // protocol,
174
- // hostname,
175
- // body,
176
- // type,
177
- // port: Number(port),
178
- // search: "?" + u.right("?"),
179
- // path: path.indexOf("/") === 0 ? path : "/" + path,
180
- // method: method || "GET",
181
- // headers: headers ? Object.assign({}, cg.headers, headers) : Object.assign({}, cg.headers),
182
- // agent: cg.proxy ? new HttpProxyAgent(cg.proxy) : null,
183
- // cookie: this.get_cookie()
184
- // }
185
- // }
186
-
187
147
  Http.prototype.post_option = function(op) {
188
148
  var param = op.body;
189
149
  var body = "";
@@ -197,7 +157,7 @@ Http.prototype.post_option = function(op) {
197
157
  } else if (type.indexOf('html') !== -1) {
198
158
  type = "text/html; charset=UTF-8";
199
159
  } else if (type.indexOf('form') !== -1) {
200
- type = "application/x-www-form-urlencoded; charset=UTF-8";
160
+ type = "application/x-www-form-urlencoded";
201
161
  } else {
202
162
  type = "text/plain; charset=UTF-8";
203
163
  }
@@ -223,7 +183,9 @@ Http.prototype.post_option = function(op) {
223
183
  }
224
184
  }
225
185
  op.body = body;
226
- op.headers['Content-Type'] = type;
186
+ if (!op.headers['Content-Type']) {
187
+ op.headers['Content-Type'] = type;
188
+ }
227
189
  op.headers['Content-Length'] = Buffer.byteLength(body);
228
190
  }
229
191
 
@@ -238,7 +200,7 @@ Http.prototype.req = async function(options) {
238
200
  this.post_option(op);
239
201
  }
240
202
  var res = await this.request(op);
241
- if (res.status.code == 302) {
203
+ if (res.status.code == 302 || res.status.code == 307) {
242
204
  var url = res.headers.location;
243
205
  var lt = res.headers['set-cookie'];
244
206
  if (lt) {
@@ -246,14 +208,13 @@ Http.prototype.req = async function(options) {
246
208
  for (var i = 0; i < len; i++) {
247
209
  this.set_cookie(lt[i]);
248
210
  }
249
- options.href = url;
250
- op = this.http_options(options);
251
- op.referer = options.href;
252
- res = await this.request(op);
253
211
  }
254
- } else {
255
- return res;
212
+ options.href = url;
213
+ op = this.http_options(options);
214
+ op.referer = options.href;
215
+ res = await this.request(op);
256
216
  }
217
+ return res;
257
218
  }
258
219
 
259
220
  Http.prototype.undata = function(encoding) {
@@ -324,8 +285,7 @@ Http.prototype.request = function(options) {
324
285
  var mh = body.match(/charset=gb2312"/i);
325
286
  if (mh) {
326
287
  body = iconv.decode(buffer, 'gb2312');
327
- }
328
- else {
288
+ } else {
329
289
  mh = body.match(/charset=GBK"/i);
330
290
  if (mh) {
331
291
  body = iconv.decode(buffer, 'GBK');
package/package.json CHANGED
@@ -1,38 +1,38 @@
1
1
  {
2
- "name": "mm_https",
3
- "version": "1.6.1",
4
- "description": "这是超级美眉http请求帮助类模块,可自动补充cookie,实现模拟登录,通讯",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "node test.js",
8
- "dev": "nodemon test.js"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/qiuwenwu/mm_https.git"
13
- },
14
- "keywords": [
15
- "myhttp",
16
- "async",
17
- "await",
18
- "promise",
19
- "add",
20
- "del",
21
- "set",
22
- "get",
23
- "query",
24
- "run",
25
- "exec"
26
- ],
27
- "author": "邱文武",
28
- "license": "ISC",
29
- "bugs": {
30
- "url": "https://github.com/qiuwenwu/mm_https/issues"
31
- },
32
- "homepage": "https://github.com/qiuwenwu/mm_https#readme",
33
- "dependencies": {
34
- "https-proxy-agent": "^7.0.5",
35
- "iconv-lite": "^0.6.3",
36
- "mm_expand": "^1.7.0"
37
- }
38
- }
2
+ "name": "mm_https",
3
+ "version": "1.6.3",
4
+ "description": "这是超级美眉http请求帮助类模块,可自动补充cookie,实现模拟登录,通讯",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js",
8
+ "dev": "nodemon test.js"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://gitee.com/qiuwenwu91/mm_https.git"
13
+ },
14
+ "keywords": [
15
+ "myhttp",
16
+ "async",
17
+ "await",
18
+ "promise",
19
+ "add",
20
+ "del",
21
+ "set",
22
+ "get",
23
+ "query",
24
+ "run",
25
+ "exec"
26
+ ],
27
+ "author": "邱文武",
28
+ "license": "ISC",
29
+ "bugs": {
30
+ "url": "https://gitee.com/qiuwenwu91/mm_https/issues"
31
+ },
32
+ "homepage": "https://gitee.com/qiuwenwu91/mm_https#readme",
33
+ "dependencies": {
34
+ "https-proxy-agent": "^7.0.6",
35
+ "iconv-lite": "^0.6.3",
36
+ "mm_expand": "^1.8.3"
37
+ }
38
+ }
package/test.js CHANGED
@@ -4,7 +4,8 @@ var fs = require("fs");
4
4
 
5
5
  async function test(){
6
6
  var http = new Http();
7
- var res = await http.get("http://8xf.91yoyoo.com/");
7
+ var urls = `https://www.yxnav.com/cms/article_view?article_id=111334\r\nhttps://www.yxnav.com/cms/article_view?article_id=111335`;
8
+ var res = await http.post("http://data.zz.baidu.com/urls?site=www.yxnav.com&token=8pMWNqhff875Juuh", urls);
8
9
  console.log("html", res);
9
10
  }
10
11