DocumentResource Class

A resource returned by DocumentLoader.LoadAsync: the response of a request, with the decoded text or the bytes of its body, its status and its headers.

public sealed class DocumentResource

Example

// A resource created from its decoded content
var page = new DocumentResource("<p>Hello</p>");
Assert.Equal("<p>Hello</p>", page.Content);

// A resource created from its bytes, decoded using the charset of the Content-Type header
var bytes = "console.log('Hello');"u8.ToArray();
var script = new DocumentResource(bytes) { Headers = [new("Content-Type", "text/javascript; charset=utf-8")] };
Assert.Null(script.Content);
Assert.Equal(bytes, script.ContentBytes?.ToArray());

Constructors

NameDescription
DocumentResource(ReadOnlyMemory<byte>)Creates a resource from its bytes. The encoding is determined as specified: the byte order mark, the charset parameter of the Content-Type header, then the <meta charset> of an HTML document (or UTF-8 for the other documents, and the encoding of the document for the scripts and style sheets).
DocumentResource(string)Creates a resource from its decoded content.

DocumentResource overloads

DocumentResource(ReadOnlyMemory<byte>)

BrowsingContextOptions.cs

Creates a resource from its bytes. The encoding is determined as specified: the byte order mark, the charset parameter of the Content-Type header, then the <meta charset> of an HTML document (or UTF-8 for the other documents, and the encoding of the document for the scripts and style sheets).

public DocumentResource(ReadOnlyMemory<byte> content)
Parameters
content ReadOnlyMemory<byte>
The bytes of the resource.

DocumentResource(string)

BrowsingContextOptions.cs

Creates a resource from its decoded content.

public DocumentResource(string content)
Parameters
content string
The decoded content of the resource.

Properties

NameDescription
ConnectionGets or sets the connection of the response to the opening handshake of a WebSocket, whose status is 101 (Switching Protocols): the stream on which the frames of the WebSocket protocol are read and written (https://www.rfc-editor.org/rfc/rfc6455#section-5), positioned after the headers of the response. See DocumentLoader.ConnectWebSocketAsync. The library disposes it. The default is null.
ContentGets the decoded content of the resource, or null when it was created from bytes.
ContentBytesGets the bytes of the resource, or null when it was created from its decoded content.
HeadersGets or sets the headers of the response, in their order (https://fetch.spec.whatwg.org/#concept-response-header-list). The names are compared ASCII case-insensitively, and the values of headers with the same name are combined as specified. The default is no header.
RedirectUrlsGets or sets the URLs of the redirects between the requested URL and DocumentResource.Url, in order (the URL list of the response without its first and last URLs, https://fetch.spec.whatwg.org/#concept-response-url-list). A redirect to another origin taints the response of a no-CORS request, e.g. the rules of a style sheet cannot be accessed. The default is no redirect.
RemainingContentBytesGets or sets the rest of the body of a response that arrives over time (e.g. a chunked response), after the bytes of DocumentResource.ContentBytes, or null when the body is complete. The HTML document of a navigation is parsed as its chunks arrive: the parser waits for the next chunk at the end of the bytes received so far (and, when the response does not declare the encoding, for the bytes that determine it), and the document is complete at the end of the sequence. The events of an EventSource are dispatched as the chunks of its event stream arrive (the sequence is enumerated with the cancellation token of the request, which is canceled when the event source is closed). The other requests, and the navigations to a document that is not an HTML document, wait for the end of the sequence and get the complete body; an HTML document created from its decoded content ignores the sequence. The enumerator is disposed when the body is not needed (e.g. a navigation whose response does not create a document).
StatusCodeGets or sets the HTTP status code of the response (https://fetch.spec.whatwg.org/#concept-response-status). The default is 200. The body of a document is displayed whatever its status (e.g. a 404 page), except for the 204 and 205 statuses, which do not create a document. A script or an imported style sheet whose status is not in the range 200-299 is a network error.
StatusTextGets or sets the status message of the response (https://fetch.spec.whatwg.org/#concept-response-status-message), e.g. OK (the statusText of fetch() and XMLHttpRequest). The default is null: the reason phrase of DocumentResource.StatusCode.
UrlGets or sets the URL of the response (e.g. after redirects). The default is the requested URL.

Gets or sets the connection of the response to the opening handshake of a WebSocket, whose status is 101 (Switching Protocols): the stream on which the frames of the WebSocket protocol are read and written (https://www.rfc-editor.org/rfc/rfc6455#section-5), positioned after the headers of the response. See DocumentLoader.ConnectWebSocketAsync. The library disposes it. The default is null.

public Stream? Connection { get; init; }

Property value

Stream?

Gets the decoded content of the resource, or null when it was created from bytes.

public string? Content { get; }

Property value

string?

Gets the bytes of the resource, or null when it was created from its decoded content.

public ReadOnlyMemory<byte>? ContentBytes { get; }

Property value

ReadOnlyMemory<byte>?

Gets or sets the headers of the response, in their order (https://fetch.spec.whatwg.org/#concept-response-header-list). The names are compared ASCII case-insensitively, and the values of headers with the same name are combined as specified. The default is no header.

public IReadOnlyList<KeyValuePair<string, string>> Headers { get; init; }

Property value

IReadOnlyList<KeyValuePair<string, string>>

Remarks

The Content-Type header is the MIME type of the resource, possibly with parameters (e.g. text/html; charset=utf-8). The document of a navigation is created from text/html (the default when there is no valid Content-Type), an XML MIME type (e.g. application/xhtml+xml or image/svg+xml) or text/plain; the other types create a document without content.

The other headers used by the library are listed in the documentation of DocumentLoader.

Gets or sets the URLs of the redirects between the requested URL and DocumentResource.Url, in order (the URL list of the response without its first and last URLs, https://fetch.spec.whatwg.org/#concept-response-url-list). A redirect to another origin taints the response of a no-CORS request, e.g. the rules of a style sheet cannot be accessed. The default is no redirect.

public IReadOnlyList<string> RedirectUrls { get; init; }

Property value

IReadOnlyList<string>

RemainingContentBytes

BrowsingContextOptions.cs

Gets or sets the rest of the body of a response that arrives over time (e.g. a chunked response), after the bytes of DocumentResource.ContentBytes, or null when the body is complete. The HTML document of a navigation is parsed as its chunks arrive: the parser waits for the next chunk at the end of the bytes received so far (and, when the response does not declare the encoding, for the bytes that determine it), and the document is complete at the end of the sequence. The events of an EventSource are dispatched as the chunks of its event stream arrive (the sequence is enumerated with the cancellation token of the request, which is canceled when the event source is closed). The other requests, and the navigations to a document that is not an HTML document, wait for the end of the sequence and get the complete body; an HTML document created from its decoded content ignores the sequence. The enumerator is disposed when the body is not needed (e.g. a navigation whose response does not create a document).

public IAsyncEnumerable<ReadOnlyMemory<byte>>? RemainingContentBytes { get; init; }

Gets or sets the HTTP status code of the response (https://fetch.spec.whatwg.org/#concept-response-status). The default is 200. The body of a document is displayed whatever its status (e.g. a 404 page), except for the 204 and 205 statuses, which do not create a document. A script or an imported style sheet whose status is not in the range 200-299 is a network error.

public int StatusCode { get; init; }

Property value

int

Gets or sets the status message of the response (https://fetch.spec.whatwg.org/#concept-response-status-message), e.g. OK (the statusText of fetch() and XMLHttpRequest). The default is null: the reason phrase of DocumentResource.StatusCode.

public string? StatusText { get; init; }

Property value

string?

Gets or sets the URL of the response (e.g. after redirects). The default is the requested URL.

public string? Url { get; init; }

Property value

string?