refactor: http_util

This commit is contained in:
手瓜一十雪 2024-05-13 17:41:10 +08:00
parent 3692d1499f
commit 317c6d96e3
2 changed files with 19 additions and 55 deletions

View File

@ -1,57 +1,21 @@
const https = require('node:https');
export async function HttpGetCookies(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);
export class RequestUtil {
static async HttpGetCookies(url: string, method: string = 'GET'): Promise<Map<string, string>> {
const response = await fetch(url, { method: method });
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.statusText}`);
}
});
});
req.on('error', (error: any) => {
resolve(result);
// console.log(error)
});
req.end();
});
const cookiesHeader = response.headers.get('set-cookie');
if (!cookiesHeader) {
return new Map<string, string>();
}
const result = new Map<string, string>();
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();
});
}

@ -1 +1 @@
Subproject commit 7575cec4fe527796a72126097e6feb6139799e27
Subproject commit f76362e1e894e3c6498f7709ce9f3b1a7c9fe836