【问题标题】:Electron GET request with saved session保存会话的电子 GET 请求
【发布时间】:2021-10-04 11:28:47
【问题描述】:

在 BrowserWindow 中手动登录网站并使用下面的 webPreferences.partition 选项保持会话后,我尝试使用 net.request 模块向网站发送 GET 请求。但是,net.request 将登录页面返回给我,而不是登录页面,如果已登录,则表明它没有使用指示的分区(会话选项也失败)。这是一个错误还是我使用不正确?

// From main.js open browser window and login manually
let loginWindow = new BrowserWindow({
    width: 800,
    height: 600,
    title: 'Login Manually',
    show: show,
    webPreferences: {
        partition: 'persist:my-session-name'
    }
});

loginWindow.loadURL('https://my.internal.url/path');

// later in the main process, try to hit landing page or another endpoint using net.request and the same session
const net = require('electron').net;

const request = net.request({
    method: 'GET',
    url: 'https://my.internal.url/path',
    // 'persist:my-session-name' was created by logging in manually earlier
    // Failes with 'persist:my-session-name' and 'my-session-name'
    partition: 'persist:my-session-name'
});

request.on('response', (response) => {
    console.log(`STATUS: ${response.statusCode}`);

    response.on('end', () => {
        console.log('No more data in response.');
    });

    response.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
    });


});

request.end();

【问题讨论】:

    标签: javascript node.js session electron


    【解决方案1】:

    您又错过了一个参数:useSessionCookies。只需像这样将其添加到您的代码中:

    const request = net.request({
        method: 'GET',
        url: 'https://my.internal.url/path',
        partition: 'persist:my-session-name',
        useSessionCookies: true
    });
    

    有关更多选项和详细信息,请访问:Electron.ClientRequest

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-01
      • 2012-06-29
      • 2016-02-02
      • 1970-01-01
      • 2020-08-01
      • 2015-01-12
      • 2018-01-05
      • 2016-06-28
      相关资源
      最近更新 更多