mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
refactor: http_util
This commit is contained in:
parent
3692d1499f
commit
317c6d96e3
src
@ -1,57 +1,21 @@
|
|||||||
const https = require('node:https');
|
export class RequestUtil {
|
||||||
export async function HttpGetCookies(url: string): Promise<Map<string, string>> {
|
static async HttpGetCookies(url: string, method: string = 'GET'): Promise<Map<string, string>> {
|
||||||
return new Promise((resolve, reject) => {
|
const response = await fetch(url, { method: method });
|
||||||
const result: Map<string, string> = new Map<string, string>();
|
if (!response.ok) {
|
||||||
const req = https.get(url, (res: any) => {
|
throw new Error(`Failed to fetch: ${response.statusText}`);
|
||||||
res.on('data', (data: any) => {
|
|
||||||
});
|
|
||||||
res.on('end', () => {
|
|
||||||
try {
|
|
||||||
const responseCookies = res.headers['set-cookie'];
|
|
||||||
for (const line of responseCookies) {
|
|
||||||
const parts = line.split(';');
|
|
||||||
const [key, value] = parts[0].split('=');
|
|
||||||
result.set(key, value);
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
}
|
|
||||||
resolve(result);
|
|
||||||
|
|
||||||
});
|
const cookiesHeader = response.headers.get('set-cookie');
|
||||||
});
|
if (!cookiesHeader) {
|
||||||
req.on('error', (error: any) => {
|
return new Map<string, string>();
|
||||||
resolve(result);
|
}
|
||||||
// console.log(error)
|
|
||||||
});
|
const result = new Map<string, string>();
|
||||||
req.end();
|
cookiesHeader.split(';').forEach((cookieLine) => {
|
||||||
|
const [key, value] = cookieLine.split('=');
|
||||||
|
result.set(key.trim(), value.trim());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
export async function HttpPostCookies(url: string): Promise<Map<string, string>> {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const result: Map<string, string> = new Map<string, string>();
|
|
||||||
const req = https.get(url, (res: any) => {
|
|
||||||
res.on('data', (data: any) => {
|
|
||||||
});
|
|
||||||
res.on('end', () => {
|
|
||||||
try {
|
|
||||||
const responseCookies = res.headers['set-cookie'];
|
|
||||||
for (const line of responseCookies) {
|
|
||||||
const parts = line.split(';');
|
|
||||||
const [key, value] = parts[0].split('=');
|
|
||||||
result.set(key, value);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
}
|
|
||||||
resolve(result);
|
|
||||||
|
|
||||||
});
|
|
||||||
});
|
|
||||||
req.on('error', (error: any) => {
|
|
||||||
resolve(result);
|
|
||||||
// console.log(error)
|
|
||||||
});
|
|
||||||
req.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
2
src/core
2
src/core
@ -1 +1 @@
|
|||||||
Subproject commit 7575cec4fe527796a72126097e6feb6139799e27
|
Subproject commit f76362e1e894e3c6498f7709ce9f3b1a7c9fe836
|
Loading…
x
Reference in New Issue
Block a user