Electron - Retrieving HTTP response headers from the BrowserWindow#39;s loadUrl()(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 上下文会话的 onCompleted
或 onHeadersReceived
回调注册一个侦听器来实现此目的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 响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Electron - 从 BrowserWindow 的 loadUrl() 中检索 HTTP 响应
基础教程推荐
- 响应更改 div 大小保持纵横比 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 动态更新多个选择框 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 在for循环中使用setTimeout 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01