2.25HttpRemoteResult<TResult> return type
HttpRemoteResult<TResult> is a generic type specifically used for the response content in the HTTP remote request module. The generic parameter TResult represents the final data type to convert to; in addition to supporting common HTTP response types such as string, byte[], Stream, HttpResponseMessage, IAsyncEnumerable<T>, and IActionResult<T>, it also supports custom types and the framework's built-in VoidContent type. This type encapsulates commonly used HTTP response information and request duration, among other capabilities.
In the HTTP remote request module, all default generic request methods that do not contain the As keyword return a value of type HttpRemoteResult<TResult>. The following are examples of obtaining a return value of type HttpRemoteResult<TResult> in different ways:
// Request verb styleusing var httpResult = await httpRemoteService.GetAsync<string>("https://furion.net/");// Builder styleusing var httpResult = await httpRemoteService.SendAsync<string>(HttpRequestBuilder.Get("https://furion.net/"));HttpRemoteResult<TResult> contains the following properties and methods:
-
Properties:
ResponseMessage: the response message (HttpResponseMessagetype).ContentType: the content type (stringtype).CharSet: the character set (stringtype).ContentEncoding: the content encoding (ICollection<string>type).ContentLength: the content size (longtype).Server: the rawServerresponse header (HttpHeaderValueCollection<ProductInfoHeaderValue>type).RawSetCookies: the rawSet-Cookieresponse header collection (List<string>type).SetCookies: the responseCookiecollection (IList<SetCookieHeaderValue>type).StatusCode: the response status code (HttpStatusCodetype).IsSuccessStatusCode: whether the request succeeded (booltype).Result: the target data (TResultgeneric type).RequestDuration: the request duration in milliseconds (longtype).Headers: the response headers (HttpResponseHeaderstype).ContentHeaders: the response content headers (HttpContentHeaderstype).Version: theHTTPversion (Versiontype).HttpClientName: the configuration name of theHttpClientinstance (string?type).
-
Methods:
ToString(): outputs an indented detailed request and response information string.
In the latest version, the framework introduced support for deconstruction for the HttpRemoteResult<TResult> type. The deconstruction expression simplifies the object parsing process, making it more convenient to obtain key property values. The following is the sample code:
// Deconstruction expression for extracting the required property valuesvar (result, response) = await httpRemoteService.GetAsync<string>("https://furion.net/"); // You can call ThrowIfNull()/OrDefault() to resolve null reference warningsvar (result, response, isSuccess) = await httpRemoteService.GetAsync<string>("https://furion.net/"); // You can call ThrowIfNull()/OrDefault() to resolve null reference warningsvar (result, response, isSuccess, statusCode) = await httpRemoteService.GetAsync<string>("https://furion.net/"); // You can call ThrowIfNull()/OrDefault() to resolve null reference warningsIn these examples, result is of type TResult, response is of type HttpResponseMessage, isSuccess is of type bool, and statusCode is of type HttpStatusCode.
Using deconstruction expressions not only improves code readability but also makes the development process more efficient. This improvement allows developers to directly access the required data, reducing the steps of manually obtaining individual property values and making the code more concise and intuitive.
In addition, the HttpRemoteResult<TResult> type has a built-in ToString() method that can clearly print the detailed request header and response header information in an indented format, as shown below:
Console.WriteLine(httpResult.ToString()); // Or use Console.WriteLine(httpResult);The terminal console output is as follows:
Request Headers: User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0 traceparent: 00-602c9070b85da9bd73fc1eac36fdb3cb-14dded89e0f5266b-00General: Request URL: https://furion.net/ Request Method: GET Status Code: 200 OK HTTP Version: 1.1 HTTP Content: Content Type: HttpClient Name: Request Duration (ms): 133.00Response Headers: Server: nginx/1.22.1 Date: Mon, 18 Nov 2024 21:26:06 GMT Connection: keep-alive Vary: Accept-Encoding ETag: "67091697-f32f" Cache-Control: max-age=315360000 Accept-Ranges: bytes Content-Type: text/html Content-Length: 62255 Last-Modified: Fri, 11 Oct 2024 12:14:15 GMT Expires: Thu, 31 Dec 2037 23:55:55 GMT