2.25HttpRemoteResult<TResult> 返回类型
HttpRemoteResult 是一个泛型类型,专门用于 HTTP 远程请求模块中的响应内容。泛型参数 TResult 代表最终需要转换成的数据类型,除了支持常见的 HTTP 响应类型如 string、byte[]、Stream、HttpResponseMessage、IAsyncEnumerable 和 IActi
HttpRemoteResult<TResult> 是一个泛型类型,专门用于 HTTP 远程请求模块中的响应内容。泛型参数 TResult 代表最终需要转换成的数据类型,除了支持常见的 HTTP 响应类型如 string、byte[]、Stream、HttpResponseMessage、IAsyncEnumerable<T> 和 IActionResult<T>,还支持自定义类型和框架内置的 VoidContent 类型。该类型封装了常用的 HTTP 响应信息和请求耗时等功能。
在 HTTP 远程请求模块中,所有默认的不包含 As 关键字的泛型请求方法返回值均为 HttpRemoteResult<TResult> 类型。以下是通过不同方式获取 HttpRemoteResult<TResult> 类型返回值的示例:
// 请求谓词方式using var httpResult = await httpRemoteService.GetAsync<string>("https://furion.net/");// 构建器方式using var httpResult = await httpRemoteService.SendAsync<string>(HttpRequestBuilder.Get("https://furion.net/"));HttpRemoteResult<TResult> 包含以下属性和方法:
-
属性:
ResponseMessage:响应消息(HttpResponseMessage类型)。ContentType:内容类型(string类型)。CharSet:字符集(string类型)。ContentEncoding:内容编码(ICollection<string>类型)。ContentLength:内容大小(long类型)。Server:原始响应标头Server(HttpHeaderValueCollection<ProductInfoHeaderValue>类型)。RawSetCookies:原始响应标头Set-Cookie集合(List<string>类型)。SetCookies:响应Cookie集合(IList<SetCookieHeaderValue>类型)。StatusCode:响应状态码(HttpStatusCode类型)。IsSuccessStatusCode:是否请求成功(bool类型)。Result:目标数据(TResult泛型类型)。RequestDuration:请求耗时(毫秒)(long类型)。Headers:响应标头(HttpResponseHeaders类型)。ContentHeaders:响应内容标头(HttpContentHeaders类型)。Version:HTTP版本(Version类型)。HttpClientName:HttpClient实例的配置名称(string?类型)。
-
方法:
ToString():输出带缩进的详细请求和响应信息字符串。
在最新版本中,框架为 HttpRemoteResult<TResult> 类型引入了对解构函数的支持,通过解构表达式简化对象解析过程,使得获取关键属性值变得更加便捷。以下是示例代码:
// 解构表达式用于提取必需的属性值var (result, response) = await httpRemoteService.GetAsync<string>("https://furion.net/"); // 可调用 ThrowIfNull()/OrDefault() 解决空引用警告问题var (result, response, isSuccess) = await httpRemoteService.GetAsync<string>("https://furion.net/"); // 可调用 ThrowIfNull()/OrDefault() 解决空引用警告问题var (result, response, isSuccess, statusCode) = await httpRemoteService.GetAsync<string>("https://furion.net/"); // 可调用 ThrowIfNull()/OrDefault() 解决空引用警告问题在这几个例子中,result 是 TResult 类型,response 是 HttpResponseMessage 类型,isSuccess 是 bool 类型,而 statusCode 则是 HttpStatusCode 类型。
通过使用解构表达式,不仅提升了代码的可读性,也让开发过程更加高效。这种改进允许开发者直接访问所需的数据,减少了手动获取各个属性值的步骤,从而使代码更简洁、直观。
此外,HttpRemoteResult<TResult> 类型还内置了一个 ToString() 方法,该方法能够以缩进格式清晰地打印出请求标头和响应标头的详细信息,如下所示:
Console.WriteLine(httpResult.ToString()); // 或使用 Console.WriteLine(httpResult);终端控制台输出如下:
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