Skip to main content

Request

每当页面发送网络资源请求时,[页面] 会触发以下一系列事件:

🌐 Whenever the page sends a request for a network resource the following sequence of events are emitted by Page:

如果请求在某个环节失败,那么会触发 Page.RequestFailed 事件,而不是触发 'requestfinished' 事件(也可能不是触发 'response' 事件)。

🌐 If request fails at some point, then instead of 'requestfinished' event (and possibly instead of 'response' event), the Page.RequestFailed event is emitted.

note

HTTP 错误响应,例如 404 或 503,从 HTTP 的角度来看仍然是成功的响应,因此请求将以 'requestfinished' 事件完成。:::

🌐 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.


方法

🌐 Methods

AllHeadersAsync

Added in: v1.15 request.AllHeadersAsync

一个包含与此请求关联的所有 HTTP 请求头的对象。头部名称为小写。

🌐 An object with all the request HTTP headers associated with this request. The header names are lower-cased.

用法

await Request.AllHeadersAsync();

返回


Failure

Added before v1.9 request.Failure

除非该请求失败并由 requestfailed 事件报告,否则该方法返回 null

🌐 The method returns null unless this request has failed, as reported by requestfailed event.

用法

记录所有失败请求的示例:

🌐 Example of logging of all the failed requests:

page.RequestFailed += (_, request) =>
{
Console.WriteLine(request.Failure);
};

返回


Frame

Added before v1.9 request.Frame

返回发起此请求的 Frame

🌐 Returns the Frame that initiated this request.

用法

var frameUrl = request.Frame.Url;

返回

详情

请注意,在某些情况下,框架不可用,此方法将抛出异常。

🌐 Note that in some cases the frame is not available, and this method will throw.

  • 当请求源自服务工作者时。你可以使用 request.serviceWorker() 来检查这一点。
  • 当导航请求在相应的框架创建之前发出时,可以使用 Request.IsNavigationRequest 来检查。

这是一个处理所有情况的示例:

🌐 Here is an example that handles all the cases:


HeaderValueAsync

Added in: v1.15 request.HeaderValueAsync

返回与名称匹配的头部的值。名称不区分大小写。

🌐 Returns the value of the header matching the name. The name is case-insensitive.

用法

await Request.HeaderValueAsync(name);

参数

返回


Headers

Added before v1.9 request.Headers

一个包含请求 HTTP 头的对象。头名称为小写。请注意,该方法不会返回与安全相关的头信息,包括与 Cookie 相关的头信息。你可以使用 Request.AllHeadersAsync() 获取包含 cookie 信息的完整头列表。

🌐 An object with the request HTTP headers. The header names are lower-cased. Note that this method does not return security-related headers, including cookie-related ones. You can use Request.AllHeadersAsync() for complete list of headers that include cookie information.

用法

Request.Headers

返回


HeadersArrayAsync

Added in: v1.15 request.HeadersArrayAsync

一个包含与此请求相关的所有请求 HTTP 头的数组。与 Request.AllHeadersAsync() 不同,头名称不会被转换为小写。具有多个条目的头,例如 Set-Cookie,会在数组中出现多次。

🌐 An array with all the request HTTP headers associated with this request. Unlike Request.AllHeadersAsync(), header names are NOT lower-cased. Headers with multiple entries, such as Set-Cookie, appear in the array multiple times.

用法

await Request.HeadersArrayAsync();

返回


IsNavigationRequest

Added before v1.9 request.IsNavigationRequest

该请求是否驱动框架的导航。

🌐 Whether this request is driving frame's navigation.

有些导航请求是在对应的帧创建之前发出的,因此没有可用的 Request.Frame

🌐 Some navigation requests are issued before the corresponding frame is created, and therefore do not have Request.Frame available.

用法

Request.IsNavigationRequest

返回


Method

Added before v1.9 request.Method

请求的方法(GET、POST 等)

🌐 Request's method (GET, POST, etc.)

用法

Request.Method

返回


PostData

Added before v1.9 request.PostData

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

🌐 Request's post body, if any.

用法

Request.PostData

返回


PostDataBuffer

Added before v1.9 request.PostDataBuffer

二进制形式的请求的帖子正文(如果有)。

🌐 Request's post body in a binary form, if any.

用法

Request.PostDataBuffer

返回


PostDataJSON

Added in: v1.12 request.PostDataJSON

返回解析后的 form-urlencoded 请求体,如果有则以 JSON 作为备用。

🌐 Returns parsed request's body for form-urlencoded and JSON as a fallback if any.

当响应为 application/x-www-form-urlencoded 时,将返回一个包含这些值的键/值对象。否则,它将被解析为 JSON。

🌐 When the response is application/x-www-form-urlencoded then a key/value object of the values will be returned. Otherwise it will be parsed as JSON.

用法

Request.PostDataJSON

返回


RedirectedFrom

Added before v1.9 request.RedirectedFrom

服务器重定向到此请求的请求(如果有)。

🌐 Request that was redirected by the server to this one, if any.

当服务器响应重定向时,Playwright 会创建一个新的 Request 对象。这两个请求通过 redirectedFrom()redirectedTo() 方法连接。当发生多个服务器重定向时,可以通过反复调用 redirectedFrom() 来构建整个重定向链。

🌐 When the server responds with a redirect, Playwright creates a new Request object. The two requests are connected by redirectedFrom() and redirectedTo() methods. When multiple server redirects has happened, it is possible to construct the whole redirect chain by repeatedly calling redirectedFrom().

用法

例如,如果网站 http://example.com 重定向到 https://example.com

🌐 For example, if the website http://example.com redirects to https://example.com:

var response = await page.GotoAsync("http://www.microsoft.com");
Console.WriteLine(response.Request.RedirectedFrom?.Url); // http://www.microsoft.com

如果网站 https://google.com 没有重定向:

🌐 If the website https://google.com has no redirects:

var response = await page.GotoAsync("https://www.google.com");
Console.WriteLine(response.Request.RedirectedFrom?.Url); // null

返回


RedirectedTo

Added before v1.9 request.RedirectedTo

如果服务器以重定向响应,则浏览器触发新请求。

🌐 New request issued by the browser if the server responded with redirect.

用法

此方法与 Request.RedirectedFrom 相反:

🌐 This method is the opposite of Request.RedirectedFrom:

Console.WriteLine(request.RedirectedFrom?.RedirectedTo == request); // True

返回


ResourceType

Added before v1.9 request.ResourceType

包含渲染引擎感知到的请求的资源类型。ResourceType 将是以下之一:documentstylesheetimagemediafontscripttexttrackxhrfetcheventsourcewebsocketmanifestother

🌐 Contains the request's resource type as it was perceived by the rendering engine. ResourceType will be one of the following: document, stylesheet, image, media, font, script, texttrack, xhr, fetch, eventsource, websocket, manifest, other.

用法

Request.ResourceType

返回


ResponseAsync

Added before v1.9 request.ResponseAsync

返回匹配的 Response 对象,如果由于错误未收到响应,则返回 null

🌐 Returns the matching Response object, or null if the response was not received due to error.

用法

await Request.ResponseAsync();

返回


SizesAsync

Added in: v1.15 request.SizesAsync

返回给定请求的资源大小信息。

🌐 Returns resource size information for given request.

用法

await Request.SizesAsync();

返回

  • 尺寸#
    • requestBodySize int

      请求体(POST 数据负载)的大小,以字节为单位。如果没有请求体,则设置为 0。

    • requestHeadersSize int

      从 HTTP 请求消息开始到(并包括)正文之前的双 CRLF 的总字节数。

    • responseBodySize int

      收到的响应正文(编码)的大小(以字节为单位)。

    • responseHeadersSize int

      从 HTTP 响应消息开始到(并包括)正文之前的双 CRLF 的总字节数。


Timing

Added before v1.9 request.Timing

返回给定请求的资源计时信息。大多数计时值在响应时可用,responseEnd 在请求完成时可用。更多信息请参见 资源计时 API

🌐 Returns resource timing information for given request. Most of the timing values become available upon the response, responseEnd becomes available when request finishes. Find more information at Resource Timing API.

用法

var request = await page.RunAndWaitForRequestFinishedAsync(async () =>
{
await page.GotoAsync("https://www.microsoft.com");
});
Console.WriteLine(request.Timing.ResponseEnd);

返回

  • 时间#
    • startTime [float]

      自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的请求开始时间(以毫秒为单位)

    • domainLookupStart [float]

      浏览器开始为资源进行域名查找之前的时间。该值以相对于 startTime 的毫秒为单位,如果不可用则为 -1。

    • domainLookupEnd [float]

      浏览器启动后立即进行资源的域名查找的时间。该值以相对于 startTime 的毫秒为单位给出,如果不可用则为 -1。

    • connectStart [float]

      用户代理开始建立与服务器的连接以获取资源之前的时间。该值以相对于 startTime 的毫秒为单位,如果不可用则为 -1。

    • secureConnectionStart [float]

      浏览器开始握手过程以保护当前连接之前的时间。该值以相对于 startTime 的毫秒为单位给出,如果不可用则为 -1。

    • connectEnd [float]

      用户代理开始建立与服务器的连接以获取资源之前的时间。该值以相对于 startTime 的毫秒为单位,如果不可用则为 -1。

    • requestStart [float]

      浏览器开始从服务器、缓存或本地资源请求资源之前的时间。该值以相对于 startTime 的毫秒为单位表示,如果不可用则为 -1。

    • responseStart [float]

      浏览器从服务器、缓存或本地资源接收响应的第一个字节后立即使用时间。该值以毫秒为单位,相对于“startTime”,如果无法获得则为-1。

    • responseEnd [float]

      在浏览器接收到资源的最后一个字节之后或传输连接关闭之前(以先发生者为准)的时间。该值以相对于 startTime 的毫秒为单位表示,如果不可用则为 -1。


Url

Added before v1.9 request.Url

请求的 URL。

🌐 URL of the request.

用法

Request.Url

返回