.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)
        {

        } 


*注意:本示例类不是静态的如果没有使用注入使用时请考虑静态类
复制代码

posted on   跨界农民工  阅读(3051)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示