Electron - 从 BrowserWindow 的 loadUrl() 中检索 HTTP 响应标头 [英] Electron - Retrieving HTTP response headers from the BrowserWindow's loadUrl()

查看:37
本文介绍了Electron - 从 BrowserWindow 的 loadUrl() 中检索 HTTP 响应标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个托管在 SSO 环境中的电脑上的测试项目.我实际上指向的是托管在此环境中的服务器上的网页,我需要访问 cookie(刚刚得到它们)和请求的 http 标头.我研究了文档,似乎没有 loadUrl() 方法的回调函数,也没有我可以用来获取这些标头的选项.

I'm working on a test project hosted on a pc included in a SSO environment. I'm actually pointing to a webpage hosted on a server inside this environment and I need to access the cookies (just got them) and the http headers of the request. I studied the doc and it seems there isn't a callback function for the loadUrl() method nor an option I can use to get those headers.

参考链接

https://electronjs.org/docs/api/browser-window

loadUrl 有第二个可选参数,定义为 Electron.loadURLOption,但它似乎没有帮助

loadUrl has a second optional parameter that is defined as an Electron.loadURLOption but it doesn't seem to be helpful

interface LoadURLOptions {
    /**
     * An HTTP Referrer url.
     */
    httpReferrer?: (string) | (Referrer);
    /**
     * A user agent originating the request.
     */
    userAgent?: string;
    /**
     * Extra headers separated by "
"
     */
    extraHeaders?: string;
    postData?: (UploadRawData[]) | (UploadFile[]) | (UploadBlob[]);
    /**
     * Base url (with trailing path separator) for files to be loaded by the data url.
     * This is needed only if the specified url is a data url and needs to load other
     * files.
     */
    baseURLForDataURL?: string;
}

如果有任何帮助,我将不胜感激,谢谢

I'd appreciate any kind of help, thanks

推荐答案

您可以通过为 Web 上下文会话的 onCompletedonHeadersReceived 回调注册一个侦听器来实现此目的webRequest 属性,例如:

You can achieve this by registering a listener for the onCompleted or onHeadersReceived callback of the web context session's webRequest property, eg:

  const url = 'https://example.com/your/page';
  browserWindow.webContents.session.webRequest.onCompleted({ urls: [url] }, (details) => {
    // Access request headers via details.requestHeaders
    // Access response headers via details.responseHeaders
  });

  browserWindow.loadURL(url);

onHeadersReceived 有点棘手,因为它需要调用传递的回调才能继续页面加载过程.

The onHeadersReceived is a little trickier as it requires invoking the passed callback in order to continue the page loading process.

相关文档

这篇关于Electron - 从 BrowserWindow 的 loadUrl() 中检索 HTTP 响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆