6.20HttpRemoteResult<TResult> Return Value
HttpRemoteResult<TResult> is a generic type specifically used for response content in the HTTP remote request module. The generic parameter TResult represents the data type that needs to be converted into in the end. 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 functions such as HTTP response information and request duration.
In the HTTP remote request module, all default generic request methods that do not contain the As keyword return the HttpRemoteResult<TResult> type. The following is an example of obtaining a HttpRemoteResult<TResult> type return value in different ways:
// Request verb approachvar httpResult = await httpRemoteService.GetAsync<string>("https://furion.net/");// Builder approachvar 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 raw response headerServer(HttpHeaderValueCollection<ProductInfoHeaderValue>type).RawSetCookies: the raw response headerSet-Cookiecollection (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 configured name of theHttpClientinstance (string?type).
-
Methods:
ToString(): outputs a string with indented, detailed request and response information.
In the latest version, the framework introduced support for deconstruction for the HttpRemoteResult<TResult> type. Deconstruction expressions simplify the object parsing process, making it more convenient to obtain key property values. The following is the sample code:
// Deconstruction expressions are used to extract the required property valuesvar (result, response) = await httpRemoteService.GetAsync<string>("https://furion.net/"); // You can call ThrowIfNull()/OrDefault() to resolve the null reference warningvar (result, response, isSuccess) = await httpRemoteService.GetAsync<string>("https://furion.net/"); // You can call ThrowIfNull()/OrDefault() to resolve the null reference warningvar (result, response, isSuccess, statusCode) = await httpRemoteService.GetAsync<string>("https://furion.net/"); // You can call ThrowIfNull()/OrDefault() to resolve the null reference warningIn these examples, result is of type TResult, response is of type HttpResponseMessage, isSuccess is of type bool, and statusCode is of type HttpStatusCode.
By using deconstruction expressions, not only is code readability improved, but the development process also becomes more efficient. This improvement allows developers to directly access the data they need, reducing the steps of manually obtaining each property value, thereby making the code more concise and intuitive.
Additionally, the HttpRemoteResult<TResult> type also has a built-in ToString() method, which can clearly print out the detailed information of the request headers and response headers 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