Skip to main content

net

Issue HTTP/HTTPS requests using Chromium's native networking library使用Chromium的本地网络库发出HTTP/HTTPS请求

Process:进程:Main

The net module is a client-side API for issuing HTTP(S) requests. net模块是用于发出HTTP(S)请求的客户端API。It is similar to the HTTP and HTTPS modules of Node.js but uses Chromium's native networking library instead of the Node.js implementation, offering better support for web proxies. 它类似于Node.js的HTTPHTTPS模块,但使用Chromium的本地网络库而不是Node.js实现,为web代理提供了更好的支持。It also supports checking network status.它还支持检查网络状态。

The following is a non-exhaustive list of why you may consider using the net module instead of the native Node.js modules:以下是您可以考虑使用net模块而不是本地Node.js模块的原因的非详尽列表:

  • Automatic management of system proxy configuration, support of the wpad protocol and proxy pac configuration files.自动管理系统代理配置,支持wpad协议和代理pac配置文件。
  • Automatic tunneling of HTTPS requests.HTTPS请求的自动隧道传输。
  • Support for authenticating proxies using basic, digest, NTLM, Kerberos or negotiate authentication schemes.支持使用基本、摘要、NTLM、Kerberos或协商身份验证方案对代理进行身份验证。
  • Support for traffic monitoring proxies: Fiddler-like proxies used for access control and monitoring.支持流量监控代理:用于访问控制和监控的类似Fiddler的代理。

The API components (including classes, methods, properties and event names) are similar to those used in Node.js.API组件(包括类、方法、属性和事件名称)与Node.js中使用的组件类似。

Example usage:示例用法:

const { app } = require('electron')
app.whenReady().then(() => {
const { net } = require('electron')
const request = net.request('https://github.com')
request.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`)
console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
response.on('data', (chunk) => {
console.log(`BODY: ${chunk}`)
})
response.on('end', () => {
console.log('No more data in response.')
})
})
request.end()
})

The net API can be used only after the application emits the ready event. 只有在应用程序发出ready事件之后,才能使用net API。Trying to use the module before the ready event will throw an error.ready事件之前尝试使用模块将引发错误。

Methods方法

The net module has the following methods:net模块有以下方法:

net.request(options)

  • options (ClientRequestConstructorOptions | string) - The ClientRequest constructor options.ClientRequest构造函数选项。

Returns 返回ClientRequest

Creates a ClientRequest instance using the provided options which are directly forwarded to the ClientRequest constructor. 使用提供的选项创建ClientRequest实例,这些options将直接转发给ClientRequest构造函数。The net.request method would be used to issue both secure and insecure HTTP requests according to the specified protocol scheme in the options object.net.request方法将用于根据options对象中指定的协议方案发出安全和不安全的HTTP请求。

net.isOnline()

Returns返回boolean - Whether there is currently internet connection.当前是否有互联网连接。

A return value of false is a pretty strong indicator that the user won't be able to connect to remote sites. 返回值false是一个很强的指标,表明用户将无法连接到远程站点。However, a return value of true is inconclusive; even if some link is up, it is uncertain whether a particular connection attempt to a particular remote site will be successful.然而,返回值true是不确定的;即使某些链接已建立,也不确定到特定远程站点的特定连接尝试是否会成功。

Properties属性

net.online Readonly

A boolean property. boolean属性。Whether there is currently internet connection.当前是否有互联网连接。

A return value of false is a pretty strong indicator that the user won't be able to connect to remote sites. 返回值false是一个很强的指标,表明用户将无法连接到远程站点。However, a return value of true is inconclusive; even if some link is up, it is uncertain whether a particular connection attempt to a particular remote site will be successful.然而,返回值true是不确定的;即使某些链接已建立,也不确定到特定远程站点的特定连接尝试是否会成功。