XmlHttpRequest Class

Sends HTTP requests from a document (https://xhr.spec.whatwg.org/#interface-xmlhttprequest). The requests are fetched with the fetch algorithm of the library (https://fetch.spec.whatwg.org/#fetching: the CORS protocol, the redirects, the blob: and data: URLs), whose HTTP requests are sent by the DocumentLoader of the browsing context of the window that created the object. The events are fired by the tasks of the event loop of the window.

public sealed class XmlHttpRequest : XmlHttpRequestEventTarget

Remarks

The response body is received at once: the progress event is fired once, when the body is received. A synchronous request blocks the event loop until the loader completes. An object created without window (e.g. from .NET without script engine) cannot send requests: they are network errors.

See also

Inheritance
XmlHttpRequestEventTarget XmlHttpRequest

Constructors

NameDescription
XmlHttpRequest()Creates a request object in the XmlHttpRequest.Unsent state.

XmlHttpRequest()

XmlHttpRequest.cs

Creates a request object in the XmlHttpRequest.Unsent state.

public XmlHttpRequest()

Properties

NameDescription
OnReadyStateChangeGets or sets the event handler of the readystatechange event.
ReadyStateGets the state of the request: XmlHttpRequest.Unsent, XmlHttpRequest.Opened, XmlHttpRequest.HeadersReceived, XmlHttpRequest.Loading or XmlHttpRequest.Done.
ResponseGets the response (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-response): a string for the text types, else null until the response is received, then a Byte array (an ArrayBuffer in JavaScript), a Blob, a Document or the JSON value (parsed by the script engine).
ResponseTextGets the text of the response (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsetext).
ResponseTypeGets or sets the type of XmlHttpRequest.Response.
ResponseUrlGets the URL of the response, without its fragment, or the empty string.
ResponseXmlGets the document of the response, or null (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsexml).
StatusGets the status of the response (0 before the response is received, and for a network error).
StatusTextGets the status message of the response.
TimeoutGets or sets the timeout of the request, in milliseconds (0 for no timeout).
UploadGets the target of the progress events of the request body.
WithCredentialsGets or sets whether the credentials are included in a cross-origin request.

OnReadyStateChange

XmlHttpRequest.cs

Gets or sets the event handler of the readystatechange event.

public Action<Event>? OnReadyStateChange { get; set; }

Gets the response (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-response): a string for the text types, else null until the response is received, then a Byte array (an ArrayBuffer in JavaScript), a Blob, a Document or the JSON value (parsed by the script engine).

public object? Response { get; }

ResponseText

XmlHttpRequest.cs

Gets the text of the response (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsetext).

public string ResponseText { get; }

ResponseType

XmlHttpRequest.cs

Gets the URL of the response, without its fragment, or the empty string.

public string ResponseUrl { get; }

Gets the document of the response, or null (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsexml).

public Document? ResponseXml { get; }

Gets the status of the response (0 before the response is received, and for a network error).

public ushort Status { get; }

Gets the status message of the response.

public string StatusText { get; }

Gets or sets the timeout of the request, in milliseconds (0 for no timeout).

public uint Timeout { get; set; }

Gets the target of the progress events of the request body.

public XmlHttpRequestUpload Upload { get; }

WithCredentials

XmlHttpRequest.cs

Gets or sets whether the credentials are included in a cross-origin request.

public bool WithCredentials { get; set; }

Methods

NameDescription
Abort()Cancels the request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-abort).
GetAllResponseHeaders()Returns the response headers, sorted by name (lowercase), one per line (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-getallresponseheaders).
GetResponseHeader(string)Returns the values of the response headers with the name, separated by ", ", or null.
Open(string, string)Initializes an asynchronous request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-open).
Open(string, string, bool, string?, string?)Initializes a request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-open).
OverrideMimeType(string)Overrides the MIME type of the response (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-overridemimetype).
Send(object?)Sends the request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-send). The body is null, a Document, a string, a Blob, a FormData, a UrlSearchParams, or bytes (a Byte array or a ReadOnlyMemory<T> of bytes).
SetRequestHeader(string, string)Adds a header to the request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-setrequestheader).

Cancels the request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-abort).

public void Abort()

GetAllResponseHeaders()

XmlHttpRequest.cs

Returns the response headers, sorted by name (lowercase), one per line (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-getallresponseheaders).

public string GetAllResponseHeaders()

GetResponseHeader(string)

XmlHttpRequest.cs

Returns the values of the response headers with the name, separated by ", ", or null.

public string? GetResponseHeader(string name)

Open overloads

Open(string, string)

XmlHttpRequest.cs

Initializes an asynchronous request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-open).

public void Open(string method, string url)

Open(string, string, bool, string?, string?)

XmlHttpRequest.cs

Initializes a request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-open).

public void Open(string method, string url, bool async, string? username = null, string? password = null)

OverrideMimeType(string)

XmlHttpRequest.cs

Overrides the MIME type of the response (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-overridemimetype).

public void OverrideMimeType(string mime)

Send(object?)

XmlHttpRequest.cs

Sends the request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-send). The body is null, a Document, a string, a Blob, a FormData, a UrlSearchParams, or bytes (a Byte array or a ReadOnlyMemory<T> of bytes).

public void Send(object? body = null)

SetRequestHeader(string, string)

XmlHttpRequest.cs

Adds a header to the request (https://xhr.spec.whatwg.org/#dom-xmlhttprequest-setrequestheader).

public void SetRequestHeader(string name, string value)

Constants

NameDescription
DoneThe state of a request whose operation is complete (DONE).
HeadersReceivedThe state of a request whose response headers have been received (HEADERS_RECEIVED).
LoadingThe state of a request whose response body is being received (LOADING).
OpenedThe state of a request whose open() method has been called (OPENED).
UnsentThe state of a request that is created but not opened yet (UNSENT).

Done

The state of a request whose operation is complete (DONE).

public const ushort Done = 4

HeadersReceived

The state of a request whose response headers have been received (HEADERS_RECEIVED).

public const ushort HeadersReceived = 2

Loading

The state of a request whose response body is being received (LOADING).

public const ushort Loading = 3

Opened

The state of a request whose open() method has been called (OPENED).

public const ushort Opened = 1

Unsent

The state of a request that is created but not opened yet (UNSENT).

public const ushort Unsent = 0