HTTPRequest 类
🌐 HTTPRequest class
表示页面发送的 HTTP 请求。
🌐 Represents an HTTP request sent by a page.
语法
🌐 Signature
export declare abstract class HTTPRequest
附注
🌐 Remarks
每当页面发送请求(例如请求网络资源)时,Puppeteer 的 page 会触发以下事件:
🌐 Whenever the page sends a request, such as for a network resource, the following events are emitted by Puppeteer's page:
request:当页面发送请求时触发。requestfinished- 当下载响应正文并且请求完成时触发。
如果请求在某个阶段失败,那么会发送 requestfailed 事件,而不是 requestfinished 事件。
🌐 If request fails at some point, then instead of requestfinished event the requestfailed event is emitted.
所有这些事件都提供了一个 HTTPRequest 表示发生的请求的实例:
🌐 All of these events provide an instance of HTTPRequest representing the request that occurred:
page.on('request', request => ...)
注意:HTTP 错误响应,例如 404 或 503,从 HTTP 的角度来看仍然是成功的响应,因此请求将以 requestfinished 事件完成。
🌐 NOTE: HTTP Error responses, such as 404 or 503, are still successful responses from HTTP standpoint, so request will complete with requestfinished event.
如果请求收到“重定向”响应,请求将通过 requestfinished 事件成功完成,并且会向重定向的 URL 发出一个新的请求。
🌐 If request gets a 'redirect' response, the request is successfully finished with the requestfinished event, and a new request is issued to a redirected url.
这个类的构造函数被标记为内部。第三方代码不应直接调用构造函数或创建继承 HTTPRequest 类的子类。
🌐 The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the HTTPRequest class.
属性
🌐 Properties
属性 🌐 Property | 修饰符 🌐 Modifiers | 类型 🌐 Type | 描述 🌐 Description |
|---|---|---|---|
| client |
| (实验性) 警告!使用此客户端可能会破坏 Puppeteer。请谨慎使用。 |
方法
🌐 Methods
方法 🌐 Method | 修饰符 🌐 Modifiers | 描述 🌐 Description |
|---|---|---|
| abort(errorCode, priority) | 中止请求。 🌐 Aborts a request. 附注 要使用此功能,应通过 Page.setRequestInterception() 启用请求拦截。如果未启用,此方法将立即抛出异常。 🌐 To use this, request interception should be enabled with Page.setRequestInterception(). If it is not enabled, this method will throw an exception immediately. | |
| abortErrorReason() | 最近中止请求的原因 🌐 The most recent reason for aborting the request | |
| continue(overrides, priority) | 通过可选的请求覆盖继续请求。 🌐 Continues request with optional request overrides. 附注 要使用此功能,应通过 Page.setRequestInterception() 启用请求拦截。 🌐 To use this, request interception should be enabled with Page.setRequestInterception(). 如果未启用请求拦截,则立即抛出异常。 🌐 Exception is immediately thrown if the request interception is not enabled. | |
| continueRequestOverrides() | 如果允许拦截继续(即没有调用 🌐 The | |
| enqueueInterceptAction(pendingHandler) | 将一个异步请求处理程序添加到处理队列中。延迟处理程序不能保证按特定顺序执行,但它们保证在请求拦截完成之前解决。 🌐 Adds an async request handler to the processing queue. Deferred handlers are not guaranteed to execute in any particular order, but they are guaranteed to resolve before the request interception is finalized. | |
| failure() | 访问有关请求失败的信息。 🌐 Access information about the request's failure. 附注 | |
| fetchPostData() | 从浏览器获取请求的 POST 数据。 🌐 Fetches the POST data for the request from the browser. | |
| finalizeInterceptions() | 等待挂起的拦截处理程序,然后决定如何完成请求拦截。 🌐 Awaits pending interception handlers and then decides how to fulfill the request interception. | |
| frame() | 发起请求的框架,如果导航到错误页面,则为 null。 🌐 The frame that initiated the request, or null if navigating to error pages. | |
| hasPostData() | 当请求包含 POST 数据时为真。请注意,当数据过长或无法以解码形式立即获取时,即使此标志为真,HTTPRequest.postData() 仍可能未定义。在这种情况下,请使用 HTTPRequest.fetchPostData()。 🌐 True when the request has POST data. Note that HTTPRequest.postData() might still be undefined when this flag is true when the data is too long or not readily available in the decoded form. In that case, use HTTPRequest.fetchPostData(). | |
| headers() | 一个与请求相关联的 HTTP 头对象。所有头名称都是小写。 🌐 An object with HTTP headers associated with the request. All header names are lower-case. | |
| initiator() | 请求的发起者。 🌐 The initiator of the request. | |
| interceptResolutionState() | 描述当前解析操作和优先级的 InterceptResolutionState 对象。 🌐 An InterceptResolutionState object describing the current resolution action and priority. InterceptResolutionState 包含:action: InterceptResolutionAction priority?: 数字 🌐 InterceptResolutionState contains: action: InterceptResolutionAction priority?: number InterceptResolutionAction 是以下之一: 🌐 InterceptResolutionAction is one of: | |
| isInterceptResolutionHandled() | 如果截距解析已经处理,则为 🌐 Is | |
如果请求是当前帧导航的驱动程序,则为 True。 🌐 True if the request is the driver of the current frame's navigation. | ||
| method() | 使用的方法( 🌐 The method used ( | |
| postData() |
| 已弃用: 使用 HTTPRequest.fetchPostData() 。 🌐 Use HTTPRequest.fetchPostData(). |
| redirectChain() |
🌐 A 附注
例如,如果网站 🌐 For example, if the website 如果网站 🌐 If the website | |
| resourceType() | 包含渲染引擎感知到的请求的资源类型。 🌐 Contains the request's resource type as it was perceived by the rendering engine. | |
| respond(response, priority) | 使用给定的响应来满足请求。 🌐 Fulfills a request with the given response. 附注 要使用此功能,应通过 Page.setRequestInterception() 启用请求拦截。 🌐 To use this, request interception should be enabled with Page.setRequestInterception(). 如果未启用请求拦截,则立即抛出异常。 🌐 Exception is immediately thrown if the request interception is not enabled. | |
| response() | 一个匹配的 🌐 A matching | |
| responseForRequest() | 如果允许拦截器响应(即没有调用 🌐 The | |
| url() | 请求的 URL 🌐 The URL of the request |