mirror of
https://github.com/shanmiteko/LotteryAutoScript.git
synced 2026-06-04 21:01:17 +08:00
feat: 请求错误时重传
This commit is contained in:
parent
e161ba875a
commit
69f228330e
@ -25,6 +25,51 @@
|
||||
|
||||
const { request } = require('https');
|
||||
const { stringify } = require('querystring');
|
||||
/**超时时间 */
|
||||
const timeout = 10000;
|
||||
/**超时尝试次数 */
|
||||
let retry = 5;
|
||||
/**
|
||||
* @description 简化HTTP请求
|
||||
* @param {RequestOptions} detail
|
||||
* @returns {void}
|
||||
*/
|
||||
const HttpRequest = detail => {
|
||||
const { method, url, query = {}, contents = {}, headers = {}, success, failure = e => console.warn(e) } = detail;
|
||||
if (!isHttpR(url)) return failure('非http请求');
|
||||
const
|
||||
thisURL = new URL(url),
|
||||
content = formatContents(headers, contents);
|
||||
let options = {
|
||||
timeout,
|
||||
method,
|
||||
host: thisURL.host,
|
||||
path: thisURL.pathname,
|
||||
headers,
|
||||
};
|
||||
switch (method) {
|
||||
case 'GET':
|
||||
if (Object.keys(query).length !== 0) options.path += '?' + stringify(query);
|
||||
break;
|
||||
case 'POST':
|
||||
options.headers['content-length'] = Buffer.byteLength(content, 'utf-8').toString();
|
||||
break;
|
||||
default:
|
||||
return failure(`未实现的请求方法: ${method}`);
|
||||
}
|
||||
const req = request(options, res => resDataHandler(res, success, failure));
|
||||
if (method === 'POST') req.write(content);
|
||||
req.on('timeout', req.end)
|
||||
.on('error', err => {
|
||||
if (retry--) {
|
||||
console.log('请求失败,尝试重新连接');
|
||||
HttpRequest(detail);
|
||||
} else {
|
||||
failure(`请求失败: ${err.message}`);
|
||||
}
|
||||
})
|
||||
.end();
|
||||
}
|
||||
/**
|
||||
* 处理请求体
|
||||
* 默认url编码字符串
|
||||
@ -68,39 +113,6 @@ function resDataHandler(res, success, failure) {
|
||||
failure(`网络错误: 错误码${statusCode}`);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @description 简化HTTP请求
|
||||
* @param {RequestOptions} detail
|
||||
* @returns {void}
|
||||
*/
|
||||
const HttpRequest = detail => {
|
||||
const { method, url, query = {}, contents = {}, headers = {}, success, failure = e => console.warn(e) } = detail;
|
||||
if (!isHttpR(url)) return failure('非http请求');
|
||||
const
|
||||
thisURL = new URL(url),
|
||||
content = formatContents(headers, contents);
|
||||
let options = {
|
||||
method,
|
||||
host: thisURL.host,
|
||||
path: thisURL.pathname,
|
||||
headers,
|
||||
};
|
||||
switch (method) {
|
||||
case 'GET':
|
||||
if (Object.keys(query).length !== 0) options.path += '?' + stringify(query);
|
||||
break;
|
||||
case 'POST':
|
||||
options.headers['content-length'] = Buffer.byteLength(content, 'utf-8').toString();
|
||||
break;
|
||||
default:
|
||||
return failure(`未实现的请求方法: ${method}`);
|
||||
}
|
||||
const req = request(options, res => resDataHandler(res, success, failure));
|
||||
if (method === 'POST') req.write(content);
|
||||
req.setTimeout(5000, req.abort)
|
||||
.on('error', err => failure(`请求失败: ${err.message}`))
|
||||
.end();
|
||||
}
|
||||
module.exports = {
|
||||
HttpRequest
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user