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 控制台下使用HttpClientFactory封装的更多相关文章

  1. eclipse控制台下实现jdbc简单的增删改查测试

    1.现在MySQL中创建一个表 2.首先创建一个类 //导入的包 import java.sql.Connection;import java.sql.DriverManager;import jav ...

  2. IE 、Firefox、Chrome 浏览器在 F12 控制台下切换至不同框架介绍

    有不少网页的页面,还在使用 iframe 标签,而此时,相当于页面有两个 window 对象,一个为当前页面 window ,另一个则为 iframe 页面下的 window .因为,有时候需要在 c ...

  3. 控制台下的计算器——C++实现

    最近这段时间看操作系统的东西看的头晕脑胀的,所以先停个一晚上,写个控制台下的计算器,来练练栈的使用. 首先,分析一下要完成的东西.输入肯定使用string类来输入的,然后第一步,就是把string的字 ...

  4. QT在Windows控制台下输出

    原地址:http://blog.csdn.net/fjb2080/article/details/9013047 在windows的控制台下输出,需要在pro文件中加入: CONFIG += cons ...

  5. Windows控制台下绘制简单图形

    最近接触到一个很有意思的问题,如何在Windows控制台下画图,翻遍了C的头文件也没找到画图的函数,好吧,那就用Windows提供的API函数吧,看来想移植是没戏了.先画一个简单的图,类似心电图那种吧 ...

  6. C#控制台下测试多线程的源码

    下边代码是关于C#控制台下测试多线程的的代码,应该是对小伙伴有所用. class Program { static void Main(string[] args) { ThreadStart num ...

  7. 网络基础 Windows控制台下Ftp使用简介

    Windows控制台下Ftp使用简介 by:授客 QQ:1033553122 测试环境: ftp服务器所在主机ip:172.25.75.2 ftp用户目录:F:\ftp   C:\Users\laif ...

  8. .net core 中使用httpclient,HttpClientFactory的问题

    Microsoft 在.Net Framework 4.5中引入了HttpClient,并且是在.NET服务器端代码中使用Web API的最常用方法.但它有一些严重的问题,如释放HttpClient对 ...

  9. Linux在终端和控制台下复制粘贴命令快捷键

    1.在终端下: (1)复制命令:Ctrl + Shift + C 组合键. (2)粘贴命令:Ctrl + Shift + V 组合键. 2.在控制台下:(即vi编辑过程中) (1)复制命令:Ctrl ...

随机推荐

  1. 【CodeVS】1083 Cantor表

    1083 Cantor表 1999年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 现代数学的著名证明之 ...

  2. Leetcode16.3Sum Closest最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  3. 30分钟学webpack实战

    阅读目录 一:什么是webpack? 他有什么优点? 二:如何安装和配置 三:理解webpack加载器 四:理解less-loader加载器的使用 五:理解babel-loader加载器的含义 六:了 ...

  4. QT_获取正在运行程序的进程id(判断程序是否正在运行)

    bool checkProcessRunning(const QString &processName, QList<quint64> &listProcessId) { ...

  5. CLTPHP5.0发布

    CLTPHP内容管理系统,包含系统设置,权限管理,模型管理,数据库管理,栏目管理,会员管理,网站功能,模版管理,微信管理等相关模块.   CLTPHP内容管理系统=ThinkPHP5+layuiAdm ...

  6. CB Insights,201608月174家独角兽榜单出炉,上榜的33家中国公司都是谁?

    全球最新独角兽榜单出炉,上榜的33家中国公司都是谁? Monica  2016-09-15   近日,美国市场调研公司CB Insights发布了全球独角兽榜单(估值10亿美元以上),共有来自21个国 ...

  7. loadrunner分析之-网页、网络、资源分析

    在Web Page Diagnostics(网页分析)中当在场景中打开Diagnostics菜单下的Web Page Diagnostics功能,就能得到网页分析组图.通过这个图,可以对事务的组成进行 ...

  8. day38 12-Spring的Bean的属性的注入:名称空间p

    xmlns="http://www.springframework.org/schema/beans"是默认的名称空间. xmlns:xsi="http://www.w3 ...

  9. 一文读懂JVM

    (一)JVM 基础知识 1)Java 是如何实现跨平台的? 注意:跨平台的是 Java 程序,而不是 JVM.JVM 是用 C/C++ 开发的,是编译后的机器码,不能跨平台,不同平台下需要安装不同版本 ...

  10. django查看数据库

    #views import pymysql def get_date(request): conn = pymysql.connect( host='localhost', port=3306, us ...