3.25请求分析工具

在现代化的浏览器中,通常内置了开发者工具,这些工具能够捕获并直观展示用户访问网站时的所有请求与响应数据。类似地,我们也为 HTTP 远程请求模块提供了一套分析工具。

在现代化的浏览器中,通常内置了开发者工具,这些工具能够捕获并直观展示用户访问网站时的所有请求与响应数据。类似地,我们也为 HTTP 远程请求模块提供了一套分析工具。

cs
HttpRequestBuilder.Get("https://furion.net")    .Profiler();    // 或使用 Debugger()HttpRequestBuilder.Get("https://furion.net")    .Profiler(false);   // 禁用请求分析工具,或调用任一函数:DisableProfiler()、DisableDebugger()、Debugger(false)// 获取请求分析工具的数据HttpRequestBuilder.Get("https://furion.net")    .Profiler(analyzer =>    {        Console.WriteLine(analyzer.Data);    });HttpRequestBuilder.Get("https://furion.net")    .Profiler(analyzer =>    {        Console.WriteLine(analyzer.Data);    }, false);  // 禁用请求分析工具

启用后,当执行 HTTP 远程请求时,控制台将输出如下详细信息:

bash
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    X-Header:        customGeneral:    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):     149.00Response Headers:    Server:             nginx/1.22.1    Date:               Thu, 14 Nov 2024 15:35:41 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

此外,除了为单个请求启用分析工具,还可以全局注册以在 HttpClient 中启用:

cs
// 为默认客户端启用services.AddHttpClient(string.Empty)    .AddProfilerDelegatingHandler();// 还可以提供条件禁用,例如生产环境中禁用services.AddHttpClient(string.Empty)    .AddProfilerDelegatingHandler(disableIn: () => builder.Environment.EnvironmentName == "Production");services.AddHttpClient(string.Empty)    .AddProfilerDelegatingHandler(disableInProduction: true);// 为特定客户端启用//services.AddHttpClient("weixin")//    .AddProfilerDelegatingHandler();// 还可以一键为所有客户端配置启用services.ConfigureHttpClientDefaults(clientBuilder =>    clientBuilder.AddProfilerDelegatingHandler());// 或使用 IHttpRemoteBuilder 扩展方法进行一键配置services.AddHttpRemote()    .ConfigureHttpClientDefaults(clientBuilder => clientBuilder.AddProfilerDelegatingHandler());

通过启用请求分析工具,开发者能够更直观、便捷地观察和调试 HTTP 请求,从而提升开发效率与调试准确性。