zoukankan      html  css  js  c++  java
  • .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)
            {
    
            } 


    *注意:本示例类不是静态的如果没有使用注入使用时请考虑静态类
  • 相关阅读:
    使用LR编写windows sockets协议xml报文格式脚本实战
    使用LR编写HTTP协议Json报文格式接口脚本实战
    web类协议脚本-飞机订票系统示例
    使用LR编写下载类脚本
    python算法-选择排序
    python算法-冒泡排序
    用户在浏览器中输入一个url发生的奥秘
    浅谈cookie和session
    selenium加载配置参数,让chrome浏览器不出现‘Chrome正在受到自动软件的控制’的提示语,以及后台静默模式启动自动化测试,不占用桌面的方法
    Python之文件和目录操作
  • 原文地址:https://www.cnblogs.com/rengke2002/p/11540554.html
Copyright © 2011-2022 走看看