Инструменты пользователя

Инструменты сайта


nodejs:biblioteki:axios:node-cookie-proxy-agent

Агенты HTTP и HTTPS с поддержкой файлов cookie и прокси. Поддерживаются HTTP(s) и SOCKS(v4/v5).

использование

Пример с axios с HTTP-прокси:

import axios from 'axios';
import { HttpCookieProxyAgent, HttpsCookieProxyAgent } from 'node-cookie-proxy-agent';
import { CookieJar } from 'tough-cookie';
 
// HTTP(s) proxy
(async () => {
	// initialise this first
	const jar = new CookieJar();
	const proxy = 'http://127.0.0.1:8888'; // or http://id:password@127.0.0.1:8888 to use with authentication
	const httpAgent = new HttpCookieProxyAgent(jar, proxy);
	const httpsAgent = new HttpsCookieProxyAgent(jar, proxy);
 
	// add your agents to your http client
	const axiosClient = axios.create({ httpAgent, httpsAgent });
	console.log('result: ', (await axiosClient.get('https://api.ipify.org?format=json')).data);
})().catch(err => console.error(err));

Пример с axios с прокси SOCKS:

import axios from 'axios';
import { SocksCookieProxyAgent } from 'node-cookie-proxy-agent';
import { CookieJar } from 'tough-cookie';
 
// SOCKS proxy
(async () => {
	// initialise this first
	const jar = new CookieJar();
	const agent = new SocksCookieProxyAgent(jar, 'socks://127.0.0.1:8888');
 
	// add your agent to your http client
	const axiosClient = axios.create({ httpAgent: agent, httpsAgent: agent });
	console.log('result: ', (await axiosClient.get('https://api.ipify.org?format=json')).data);
})().catch(err => console.error(err));

пример с установкой cookie и организацией модульности require

const axios = require("axios");
const { HttpCookieProxyAgent, HttpsCookieProxyAgent } = require('node-cookie-proxy-agent');
const { Cookie, CookieJar } = require('tough-cookie');
 
 
const proxy = 'http://AYhDKGiD:JdGhYgxD@31.184.199.203:53294'; // or http://id:password@127.0.0.1:8888 to use with authentication
 
const cookie = new Cookie();
cookie.key = 'test';
cookie.value = 'somethingdifferent';
 
const jar = new CookieJar();
jar.setCookie(cookie, 'https://api.ipify.org', () => { });
 
const httpAgent = new HttpCookieProxyAgent(jar, proxy);
const httpsAgent = new HttpsCookieProxyAgent(jar, proxy);
 
async function start() {
    console.log(httpAgent);
    const { data } = await axios.get('https://api.ipify.org?format=json',{
        httpAgent,
        httpsAgent,
        timeout: 60000,
    });
    console.log(data);
}
start().then(() => console.log('complete'));
nodejs/biblioteki/axios/node-cookie-proxy-agent.txt · Последние изменения: 2023/01/12 12:18 (внешнее изменение)