mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
refactor: requests
This commit is contained in:
parent
3a88c21a3b
commit
3b5902b033
@ -1,6 +1,7 @@
|
|||||||
import https from 'node:https';
|
import https from 'node:https';
|
||||||
import http from 'node:http';
|
import http from 'node:http';
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
|
import { NTQQUserApi } from '@/core';
|
||||||
export class RequestUtil {
|
export class RequestUtil {
|
||||||
// 适用于获取服务器下发cookies时获取,仅GET
|
// 适用于获取服务器下发cookies时获取,仅GET
|
||||||
static async HttpsGetCookies(url: string): Promise<{ [key: string]: string }> {
|
static async HttpsGetCookies(url: string): Promise<{ [key: string]: string }> {
|
||||||
@ -60,9 +61,9 @@ export class RequestUtil {
|
|||||||
headers: headers
|
headers: headers
|
||||||
};
|
};
|
||||||
// headers: {
|
// headers: {
|
||||||
// * 'Content-Type': 'application/json',
|
// 'Content-Type': 'application/json',
|
||||||
// * 'Content-Length': Buffer.byteLength(postData),
|
// 'Content-Length': Buffer.byteLength(postData),
|
||||||
// * },
|
// },
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const req = protocol.request(options, (res: any) => {
|
const req = protocol.request(options, (res: any) => {
|
||||||
let responseBody = '';
|
let responseBody = '';
|
||||||
@ -107,57 +108,50 @@ export class RequestUtil {
|
|||||||
static async HttpGetText(url: string, method: string = 'GET', data?: any, headers: { [key: string]: string } = {}) {
|
static async HttpGetText(url: string, method: string = 'GET', data?: any, headers: { [key: string]: string } = {}) {
|
||||||
return this.HttpGetJson<string>(url, method, data, headers, false, false);
|
return this.HttpGetJson<string>(url, method, data, headers, false, false);
|
||||||
}
|
}
|
||||||
static async HttpUploadFile(url: string, filePath: string, headers: { [key: string]: string } = {}) {
|
static async HttpUploadFileForOpenPlatform(filePath: string) {
|
||||||
// 验证URL
|
const cookies = Object.entries(await NTQQUserApi.getCookies('connect.qq.com')).map(([key, value]) => `${key}=${value}`).join('; ');
|
||||||
try {
|
|
||||||
new URL(url);
|
|
||||||
} catch (e: any) {
|
|
||||||
return Promise.reject(`Invalid URL: ${e.message}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const option = new URL(url);
|
|
||||||
const protocol = url.startsWith('https://') ? https : http;
|
|
||||||
const options = {
|
|
||||||
hostname: option.hostname,
|
|
||||||
port: option.port,
|
|
||||||
path: option.pathname + option.search,
|
|
||||||
method: 'POST',
|
|
||||||
headers: headers
|
|
||||||
};
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const req = protocol.request(options, (res: any) => {
|
var options = {
|
||||||
let responseBody = '';
|
'method': 'POST',
|
||||||
res.on('data', (chunk: string | Buffer) => {
|
'hostname': 'cgi.connect.qq.com',
|
||||||
responseBody += chunk.toString();
|
'path': '/qqconnectopen/upload_share_image',
|
||||||
// 响应体最大为10M
|
'headers': {
|
||||||
if (responseBody.length > 10 * 1024 * 1024) {
|
'Referer': 'https://cgi.connect.qq.com',
|
||||||
reject('Response body size exceeded the limit');
|
'Cookie': cookies,
|
||||||
}
|
'Accept': '*/*',
|
||||||
|
'Host': 'cgi.connect.qq.com',
|
||||||
|
'Connection': 'keep-alive',
|
||||||
|
'Content-Type': 'multipart/form-data; boundary=--------------------------800945582706338065206240'
|
||||||
|
},
|
||||||
|
'maxRedirects': 20
|
||||||
|
};
|
||||||
|
let body;
|
||||||
|
let req = https.request(options, function (res) {
|
||||||
|
let chunks: any = [];
|
||||||
|
|
||||||
|
res.on("data", function (chunk) {
|
||||||
|
chunks.push(chunk);
|
||||||
});
|
});
|
||||||
res.on('end', () => {
|
|
||||||
try {
|
res.on("end", function () {
|
||||||
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
body = Buffer.concat(chunks);
|
||||||
resolve(responseBody);
|
console.log(body.toString());
|
||||||
} else {
|
});
|
||||||
reject(`Error: HTTP Status ${res.statusCode}`);
|
|
||||||
}
|
res.on("error", function (error) {
|
||||||
} catch (parseError) {
|
console.error(error);
|
||||||
reject(parseError);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('error', (e) => {
|
var postData = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"share_image\"; filename=\"C:/1.png\"\r\nContent-Type: \"{Insert_File_Content_Type}\"\r\n\r\n" + fs.readFileSync(filePath) + "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--";
|
||||||
reject(`Request error: ${e.message}`);
|
req.setHeader('content-type', 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
|
||||||
});
|
req.write(postData);
|
||||||
|
req.end();
|
||||||
req.on('close', () => {
|
if (body) {
|
||||||
console.warn('Request closed unexpectedly');
|
resolve(JSON.parse(body));
|
||||||
});
|
} else {
|
||||||
|
reject();
|
||||||
const fileStream = fs.createReadStream(filePath);
|
}
|
||||||
fileStream.pipe(req);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user