HttpRequest
An asynchronous HTTP request with a completion signal.
HttpRequest must be inside EntityTree. One instance serves one request at a time. The Error returned by Request/RequestRaw describes startup; RequestCompleted supplies separate result, responseCode, headers and body.
Result.Success means successful transfer, not necessarily HTTP 2xx, valid JSON or a completed application operation. Validate each required layer. Bound Timeout/BodySizeLimit and define redirect policy. CancelRequest does not automatically emit a cancellation completion; coordinate UI/owner lifetime.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
HttpRequestConstructor
public HttpRequest();Properties9
AcceptGZipProperty
public bool AcceptGZip { get; set; }Requests gzip/deflate when Accept-Encoding is unspecified and handles the corresponding response Content-Encoding. A decompressed-data bound is still necessary.
AppendToDownloadFileProperty
public bool AppendToDownloadFile { get; set; }Allows appending on a suitable Partial Content response. Does not replace Range/Content-Range or existing-file identity checks.
BodySizeLimitProperty
public int BodySizeLimit { get; set; }Body-byte bound; -1 disables it. Automatic decompression also bounds the decompressed result. Define an explicit budget for external responses.
DownloadChunkSizeProperty
public int DownloadChunkSize { get; set; }The size of the buffer used and maximum bytes to read per iteration. See HttpClient.ReadChunkSize.
Set this to a lower value (e.g. 4096 for 4 KiB) when downloading small files to decrease memory usage at the cost of download speeds.
DownloadFileProperty
public string DownloadFile { get; set; }Writes the response body to a path instead of signal body storage. Successful writing does not establish HTTP 2xx or valid content; use a temporary file first.
KeepPartialDownloadProperty
public bool KeepPartialDownload { get; set; }Keeps an unfinished file when a request stops. A partial file is not a completed resource; validate length/content before use.
MaxRedirectsProperty
public int MaxRedirects { get; set; }Automatic-redirect limit; 0 disallows them, -1 disables the limit. Validate destination, method and headers across redirects.
TimeoutProperty
public double Timeout { get; set; }Timeout in seconds; 0 disables it. The internal Timer ignores TimeScale, but its processing still depends on EntityTree/pause.
UseThreadsProperty
public bool UseThreads { get; set; }Enables an internal worker if the build supports it. Does not automatically make your handlers or EntityTree access thread-safe.
Methods29
CancelRequestvoid
public void CancelRequest();Stops the active request; does not itself emit RequestCompleted for cancellation. File cleanup depends on completion and KeepPartialDownload.
GetBodySizeint
public int GetBodySize();Returns the response body length.
Note: Some Web servers may not send a body length. In this case, the value returned will be -1. If using chunked transfer encoding, the body length will also be -1.
intGetBodySizeLimitint
public int GetBodySizeLimit();intGetDownloadChunkSizeint
public int GetDownloadChunkSize();intGetDownloadFilestring
public string GetDownloadFile();stringGetDownloadedBytesint
public int GetDownloadedBytes();Returns the number of bytes this HTTPRequest downloaded.
intGetHttpClientStatusHttpClient.Status
public HttpClient.Status GetHttpClientStatus();Returns the current status of the underlying HttpClient.
HttpClient.StatusGetMaxRedirectsint
public int GetMaxRedirects();intGetTimeoutdouble
public double GetTimeout();doubleIsAcceptingGZipbool
public bool IsAcceptingGZip();boolIsAppendingToDownloadFilebool
public bool IsAppendingToDownloadFile();boolIsKeepingPartialDownloadbool
public bool IsKeepingPartialDownload();boolIsUsingThreadsbool
public bool IsUsingThreads();boolRequest2 overloads
public Error Request(string url, ReadOnlySpan<string> customHeaders, HttpClient.Method method, string requestData);Starts an HTTP request with a UTF-8 string body. Requires tree membership and an idle instance. Error.Ok does not mean an HTTP 2xx response arrived: inspect RequestCompleted.
| Name | Type | Default |
|---|---|---|
url | string | — |
customHeaders | ReadOnlySpan<string> | — |
method | HttpClient.Method | — |
requestData | string | — |
Errorpublic Error Request(string url, string[] customHeaders = null, HttpClient.Method method = HttpClient.Method.Get, string requestData = "");Starts an HTTP request with a UTF-8 string body. Requires tree membership and an idle instance. Error.Ok does not mean an HTTP 2xx response arrived: inspect RequestCompleted.
| Name | Type | Default |
|---|---|---|
url | string | — |
customHeaders | string[] | null |
method | HttpClient.Method | HttpClient.Method.Get |
requestData | string | "" |
customHeadersIf the parameter is null, then the default value is Array.Empty<string>().
ErrorRequestRaw2 overloads
public Error RequestRaw(string url, ReadOnlySpan<string> customHeaders, HttpClient.Method method, ReadOnlySpan<byte> requestDataRaw);Sends exact body bytes. Match method and Content-Type to the API. Specify explicit array types if overload resolution is ambiguous.
| Name | Type | Default |
|---|---|---|
url | string | — |
customHeaders | ReadOnlySpan<string> | — |
method | HttpClient.Method | — |
requestDataRaw | ReadOnlySpan<byte> | — |
Errorpublic Error RequestRaw(string url, string[] customHeaders = null, HttpClient.Method method = HttpClient.Method.Get, byte[] requestDataRaw = null);Sends exact body bytes. Match method and Content-Type to the API. Specify explicit array types if overload resolution is ambiguous.
| Name | Type | Default |
|---|---|---|
url | string | — |
customHeaders | string[] | null |
method | HttpClient.Method | HttpClient.Method.Get |
requestDataRaw | byte[] | null |
customHeadersIf the parameter is null, then the default value is Array.Empty<string>().
requestDataRawIf the parameter is null, then the default value is Array.Empty<byte>().
ErrorSetAcceptGZipvoid
public void SetAcceptGZip(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
SetAppendToDownloadFilevoid
public void SetAppendToDownloadFile(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetBodySizeLimitvoid
public void SetBodySizeLimit(int bytes);| Name | Type | Default |
|---|---|---|
bytes | int | — |
SetDownloadChunkSizevoid
public void SetDownloadChunkSize(int chunkSize);| Name | Type | Default |
|---|---|---|
chunkSize | int | — |
SetDownloadFilevoid
public void SetDownloadFile(string path);| Name | Type | Default |
|---|---|---|
path | string | — |
SetHttpProxyvoid
public void SetHttpProxy(string host, int port);Sets the proxy server for HTTP requests.
The proxy server is unset if host is empty or port is -1.
| Name | Type | Default |
|---|---|---|
host | string | — |
port | int | — |
SetHttpsProxyvoid
public void SetHttpsProxy(string host, int port);Sets the proxy server for HTTPS requests.
The proxy server is unset if host is empty or port is -1.
| Name | Type | Default |
|---|---|---|
host | string | — |
port | int | — |
SetKeepPartialDownloadvoid
public void SetKeepPartialDownload(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetMaxRedirectsvoid
public void SetMaxRedirects(int amount);| Name | Type | Default |
|---|---|---|
amount | int | — |
SetTimeoutvoid
public void SetTimeout(double timeout);| Name | Type | Default |
|---|---|---|
timeout | double | — |
SetTlsOptionsvoid
public void SetTlsOptions(TlsOptions clientOptions);Sets client options for HTTPS. Use verified Client options and load a private CA correctly before starting.
| Name | Type | Default |
|---|---|---|
clientOptions | TlsOptions | — |
SetUseThreadsvoid
public void SetUseThreads(bool enable);| Name | Type | Default |
|---|---|---|
enable | bool | — |
Signals and events1
RequestCompletedEvent
public event HttpRequest.RequestCompletedEventHandler RequestCompleted;Reports transfer result, HTTP code, headers and body bytes. Result.Success can accompany 404. With DownloadFile the body goes to disk while body is empty.
| Name | Type | Default |
|---|---|---|
result | long | — |
responseCode | long | — |
headers | string[] | — |
body | byte[] | — |
HttpRequest.Result
| Value | Description |
|---|---|
BodyDecompressFailed = 8 | Request failed due to an error while decompressing the response body. Possible causes include unsupported or incorrect compression format, corrupted data, or incomplete transfer. |
BodySizeLimitExceeded = 7 | Request exceeded its maximum size limit, see |
CantConnect = 2 | Request failed while connecting. |
CantResolve = 3 | Request failed while resolving. |
ChunkedBodySizeMismatch = 1 | Request failed due to a mismatch between the expected and actual chunked body size during transfer. Possible causes include network errors, server misconfiguration, or issues with chunked encoding. |
ConnectionError = 4 | Request failed due to connection (read/write) error. |
DownloadFileCantOpen = 10 | HTTPRequest couldn't open the download file. |
DownloadFileWriteError = 11 | HTTPRequest couldn't write to the download file. |
NoResponse = 6 | Request does not have a response (yet). |
RedirectLimitReached = 12 | Request reached its maximum redirect limit, see |
RequestFailed = 9 | Request failed (currently unused). |
Success = 0 | Request successful. |
Timeout = 13 | Request failed due to a timeout. If you expect requests to take a long time, try increasing the value of |
TlsHandshakeError = 5 | Request failed on TLS handshake. |