类:ClientRequest
类:ClientRequest
进行 HTTP/HTTPS 请求。
进程: 主进程, 实用进程
此类未从 'electron' 模块导出。它仅作为 Electron API 中其他方法的返回值可用。
ClientRequest 实现 Writable Stream 接口,因此它是一个 EventEmitter。
new ClientRequest(options)
options 的属性,例如 protocol、host、hostname、port 和 path,严格遵循 Node.js 中 URL 模块中所述的模型。
例如,我们可以像这样创建到 'github.com' 的相同请求
const request = net.request({
method: 'GET',
protocol: 'https:',
hostname: 'github.com',
port: 443,
path: '/'
})
实例事件
事件:'response'
返回
responseIncomingMessage - 表示 HTTP 响应消息的对象。
事件: 'login'
返回
authInfoObjectisProxybooleanschemestringhoststringportIntegerrealmstring
callbackFunctionusernamestring (可选)passwordstring (可选)
当身份验证代理请求用户凭据时发出。
预计 callback 函数会使用用户凭据进行回调
usernamestringpasswordstring
request.on('login', (authInfo, callback) => {
callback('username', 'password')
})
提供空凭据将取消请求并在响应对象上报告身份验证错误
request.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`)
response.on('error', (error) => {
console.log(`ERROR: ${JSON.stringify(error)}`)
})
})
request.on('login', (authInfo, callback) => {
callback()
})
事件:'finish'
在 request 的数据最后一块被写入 request 对象后立即发出。
事件:'abort'
当 request 被中止时发出。如果 request 已经关闭,则不会触发 abort 事件。
事件:‘error’
返回
errorError - 提供有关失败的一些信息的错误对象。
当 net 模块无法发出网络请求时发出。通常,当 request 对象发出 error 事件时,随后会发出 close 事件,并且不会提供响应对象。
事件:'close'
作为 HTTP 请求-响应事务的最后一个事件发出。close 事件表示 request 或 response 对象上都不会再发出事件。
事件:'redirect'
返回
statusCodeIntegermethod字符串redirectUrlstringresponseHeadersRecord<string, string[]>
当服务器返回重定向响应时(例如 301 Moved Permanently)发出。调用 request.followRedirect 将继续重定向。如果处理了此事件,则必须 **同步** 调用 request.followRedirect,否则请求将被取消。
实例属性
request.chunkedEncoding
一个 boolean,指定请求是否将使用 HTTP 分块传输编码。默认为 false。该属性是可读写的,但只能在第一次写入操作之前设置,因为 HTTP 标头尚未发送到网络。尝试在第一次写入后设置 chunkedEncoding 属性将引发错误。
如果需要发送大型请求体,强烈建议使用分块编码,因为数据将以小块的形式流式传输,而不是在 Electron 进程内存中内部缓冲。
实例方法
request.setHeader(name, value)
namestring - 额外的 HTTP 标头名称。valuestring - 额外的 HTTP 标头值。
添加额外的 HTTP 标头。标头名称将按原样发出,不会转换为小写。只能在第一次写入之前调用。在此之后调用此方法将引发错误。如果提供的值不是 string,则会调用其 toString() 方法来获取最终值。
应用程序无法设置某些标头。这些标头如下所列。有关受限标头的更多信息,请参阅 Chromium 的标头实用工具。
Content-LengthHostTrailer或TeUpgradeCookie2Keep-AliveTransfer-Encoding
此外,不允许将 Connection 标头设置为 upgrade。
request.getHeader(name)
namestring - 指定额外的标头名称。
返回 string - 以前设置的额外标头名称的值。
request.removeHeader(name)
namestring - 指定额外的标头名称。
删除以前设置的额外标头名称。此方法只能在第一次写入之前调用。尝试在第一次写入后调用它将引发错误。
request.write(chunk[, encoding][, callback])
chunk(string | Buffer) - 请求正文数据的块。如果它是字符串,则使用指定的编码将其转换为 Buffer。encodingstring (可选) - 用于将字符串块转换为 Buffer 对象。默认为 'utf-8'。callbackFunction (可选) - 写入操作完成后调用。
callback essentially is a dummy function introduced in the purpose of keeping similarity with the Node.js API. It is called asynchronously in the next tick after chunk content have been delivered to the Chromium networking layer. Contrary to the Node.js implementation, it is not guaranteed that chunk content have been flushed on the wire before callback is called.(callback 基本上是一个占位符函数,引入该函数的目的是保持与 Node.js API 的相似性。它在 chunk 内容被传递到 Chromium 网络层后的下一个 tick 中异步调用。与 Node.js 实现相反,不能保证在调用 callback 之前 chunk 内容已经刷新到网络。)
向请求正文添加数据块。第一次写入操作可能会导致请求标头被发送到网络。第一次写入操作后,不允许添加或删除自定义标头。
request.end([chunk][, encoding][, callback])
chunk(string | Buffer) (可选)encodingstring (可选)callbackFunction (可选)
返回 this。
发送请求数据的最后一个块。不允许后续的写入或结束操作。end 操作完成后立即发出 finish 事件。
request.abort()
取消正在进行的 HTTP 事务。如果请求已经发出了 close 事件,则中止操作将无效。否则,正在进行的事件将发出 abort 和 close 事件。此外,如果存在正在进行的响应对象,它将发出 aborted 事件。
request.followRedirect()
继续任何待处理的重定向。只能在 'redirect' 事件期间调用。
request.getUploadProgress()
返回 Object
activeboolean - 请求当前是否处于活动状态。如果为 false,则其他属性将不会被设置startedboolean - 上传是否已开始。如果为 false,则current和total都将设置为 0。currentInteger - 到目前为止已上传的字节数totalInteger - 此请求将上传的总字节数
您可以结合 POST 请求使用此方法来获取文件上传或其他数据传输的进度。