Skip to main content
Version: 22.9.0

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 - 当下载响应正文并且请求完成时触发。

    ¥request: emitted when the request is issued by the page. - requestfinished - emitted when the response body is downloaded and the request is complete.

如果请求在某个时刻失败,则将触发 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.

如果请求收到 'redirect' 响应,则该请求已成功完成并发生 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

属性

修饰符

类型

描述

client

readonly

CDPSession

(实验性)警告!使用此客户端可能会破坏 Puppeteer。谨慎使用。

方法

¥Methods

方法

修饰符

描述

abort(errorCode, priority)

中止请求。

评论:

要使用此功能,应使用 Page.setRequestInterception() 启用请求拦截。如果未启用,该方法将立即抛出异常。

abortErrorReason()

最近中止请求的原因

continue(overrides, priority)

通过可选的请求覆盖继续请求。

评论:

要使用此功能,应使用 Page.setRequestInterception() 启用请求拦截。

如果未启用请求拦截,则立即抛出异常。

continueRequestOverrides()

如果允许继续拦截(即不调用 abort()respond()),将使用 ContinueRequestOverrides

enqueueInterceptAction(pendingHandler)

将异步请求处理程序添加到处理队列。延迟处理程序不保证以任何特定顺序执行,但保证它们在请求拦截完成之前解析。

failure()

访问有关请求失败的信息。

评论:

fetchPostData()

从浏览器获取请求的 POST 数据。

finalizeInterceptions()

等待挂起的拦截处理程序,然后决定如何完成请求拦截。

frame()

发起请求的框架,如果导航到错误页面,则为 null。

hasPostData()

当请求有 POST 数据时为 true。请注意,当数据太长或不易以解码形式提供时,当此标志为真时,HTTPRequest.postData() 可能仍然未定义。在这种情况下,请使用 HTTPRequest.fetchPostData()

headers()

具有与请求关联的 HTTP 标头的对象。所有标头名称均为小写。

initiator()

请求的发起者。

interceptResolutionState()

描述当前解析操作和优先级的 InterceptResolutionState 对象。

InterceptResolutionState 包含:行动:InterceptResolutionAction 优先级?:数字

InterceptResolutionAction 是以下之一:abortrespondcontinuedisablednonealready-handled

isInterceptResolutionHandled()

如果拦截解析已处理,则为 true,否则为 false

isNavigationRequest()

如果请求是当前帧导航的驱动程序,则为 True。

method()

使用的方法(GETPOST 等)

postData()

请求的帖子正文(如果有)。

redirectChain()

redirectChain 是为获取资源而发起的请求链。

评论:

redirectChain 在同一链的所有请求之间共享。

例如,如果网站 http://example.com 有一个到 https://example.com 的重定向,则该链将包含一个请求:

const response = await page.goto('http://example.com');
const chain = response.request().redirectChain();
console.log(chain.length); // 1
console.log(chain[0].url()); // 'http://example.com'

如果网站 https://google.com 没有重定向,则链将为空:

const response = await page.goto('https://google.com');
const chain = response.request().redirectChain();
console.log(chain.length); // 0
resourceType()

包含渲染引擎感知到的请求的资源类型。

respond(response, priority)

使用给定的响应来满足请求。

评论:

要使用此功能,应使用 Page.setRequestInterception() 启用请求拦截。

如果未启用请求拦截,则立即抛出异常。

response()

匹配的 HTTPResponse 对象,如果尚未收到响应则为 null。

responseForRequest()

如果允许拦截响应(即不调用 abort()),则使用 ResponseForRequest

url()

请求的 URL