You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron中发起HTTPS GET请求遭遇-202错误,寻求解决方案

Electron请求LOL客户端HTTPS API返回-202错误的解决方案

问题场景

在Electron环境中,使用axios调用从《英雄联盟》客户端lockfile获取的HTTPS API时,持续返回-202错误,但相同请求在浏览器中可正常执行。相关代码如下:

const axios = require('axios');
const https = require('https');

const customHostname = 'localhost';
const customPort = 59244;
const username = 'riot';
const password = 'h1433c5R9ZP0f7Uba8zBWQ';
const credentials = Buffer.from(`${username}:${password}`).toString('base64');

const axiosInstance = axios.create({
  baseURL: `https://${customHostname}:${customPort}`,
  httpsAgent: new https.Agent({
    rejectUnauthorized: false
  }),
  headers: {
    'Authorization': `Basic ${credentials}`,
  },
});

const endpoint = '/lol-summoner/v1/current-summoner';

axiosInstance.get(endpoint)
.then((response) => {
  console.log(response.data);
})
.catch((error) => {
  console.error(error);
});

可行解决步骤

1. 禁用Electron的证书校验拦截

Electron默认的安全机制会覆盖Node.js的rejectUnauthorized配置,需在主进程中添加命令行开关强制绕过证书校验:

const { app } = require('electron');
app.commandLine.appendSwitch('ignore-certificate-errors');
app.commandLine.appendSwitch('allow-insecure-localhost');

注意:仅在本地调试LOL客户端API场景下使用,避免在生产环境开启。

2. 替换为Electron原生net模块

Node.js原生https模块在Electron中存在兼容性问题,改用Electron自带的net模块作为请求代理:

const { net } = require('electron');

const axiosInstance = axios.create({
  baseURL: `https://${customHostname}:${customPort}`,
  httpsAgent: new net.Agent({
    rejectUnauthorized: false
  }),
  headers: {
    'Authorization': `Basic ${credentials}`,
  },
});

若在渲染进程中调用,需通过IPC委托主进程执行请求,渲染进程直接使用net模块会存在权限限制。

3. 补充浏览器模拟请求头

浏览器会自动携带User-Agent、Origin等头信息,Electron请求中缺失这些可能被LOL客户端API拦截。添加以下请求头:

headers: {
  'Authorization': `Basic ${credentials}`,
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
  'Origin': `https://${customHostname}:${customPort}`
}

4. 实时读取lockfile凭证

LOL客户端重启后,lockfile中的端口、密码会自动变更,硬编码的参数会导致请求失效。每次请求前读取最新凭证:

const fs = require('fs');
const path = require('path');

// Windows系统lockfile路径示例,其他系统需调整路径
const lockfilePath = path.join(process.env.LOCALAPPDATA, 'Riot Games', 'Riot Client', 'Config', 'lockfile');
const lockfileContent = fs.readFileSync(lockfilePath, 'utf8');
const [, port, password] = lockfileContent.split(':');

// 基于最新凭证初始化axios实例
const axiosInstance = axios.create({
  baseURL: `https://localhost:${port}`,
  httpsAgent: new https.Agent({ rejectUnauthorized: false }),
  headers: {
    'Authorization': `Basic ${Buffer.from(`riot:${password}`).toString('base64')}`
  }
});

内容的提问来源于stack exchange,提问作者1qq5

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 23:01:33