.net core 控制台下使用HttpClientFactory封装
HttpClientFactory封装,如有错误请指出,谢谢!
using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; namespace Hx.HttpClientFactory { public class HxHttpClient:IHxHttpClient { private readonly IHttpClientFactory _clientFactory; public HxHttpClient(IHttpClientFactory clientFactory) { _clientFactory = clientFactory; } public Task<HttpResponseMessage> SendAsync(string url, string data, string contentType = "text/json") { var request = new HttpRequestMessage(HttpMethod.Post, new Uri(url)); request.Headers.Clear(); request.Content = new StringContent(data); //request.Headers.Remove("Content-Type"); request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);//("text/json"); var client = _clientFactory.CreateClient(); return client.SendAsync(request); } } }
using System; using System.Collections.Generic; using System.Text; namespace Hx.HttpClientFactory { public class HxHttpClientFactory : HxHttpClientFactoryBase<IHxHttpClient, HxHttpClient>, IHxHttpClientFactory { } }
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Text; namespace Hx.HttpClientFactory { public class HxHttpClientFactoryBase<IT, T> : IHxHttpClientFactory<IT, T> where T : class, IT where IT : class { private IHost _host; public HxHttpClientFactoryBase() { var builder = new HostBuilder() .ConfigureServices((hostContext, services) => { services.AddHttpClient(); services.AddTransient<IT, T>(); }).UseConsoleLifetime(); _host = builder.Build(); } public IT CreateHttpClient() { using (var serviceScope = _host.Services.CreateScope()) { var service = serviceScope.ServiceProvider; return service.GetRequiredService<IT>(); } } } }
using System; using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace Hx.HttpClientFactory { public interface IHxHttpClient { Task<HttpResponseMessage> SendAsync(string url, string data, string contentType = "text/json"); } }
using System; using System.Collections.Generic; using System.Text; namespace Hx.HttpClientFactory { public interface IHxHttpClientFactory<IT,T> where T : class, IT where IT : class { IT CreateHttpClient(); } public interface IHxHttpClientFactory : IHxHttpClientFactory<IHxHttpClient, HxHttpClient> { } }
using System; using System.Net.Http; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using SampleApp; class Program { static async Task<int> Main(string[] args) { IHxHttpClientFactory factory = new HxHttpClientFactory(); var client= factory.CreateHttpClient(); var t= await client.SendAsync("url", "body字符串"); if (t.IsSuccessStatusCode) { }
}
}
*注意:本示例类不是静态的如果没有使用注入使用时请考虑静态类
标签:
.net core
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现