.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 控制台下使用HttpClientFactory封装的更多相关文章
- eclipse控制台下实现jdbc简单的增删改查测试
1.现在MySQL中创建一个表 2.首先创建一个类 //导入的包 import java.sql.Connection;import java.sql.DriverManager;import jav ...
- IE 、Firefox、Chrome 浏览器在 F12 控制台下切换至不同框架介绍
有不少网页的页面,还在使用 iframe 标签,而此时,相当于页面有两个 window 对象,一个为当前页面 window ,另一个则为 iframe 页面下的 window .因为,有时候需要在 c ...
- 控制台下的计算器——C++实现
最近这段时间看操作系统的东西看的头晕脑胀的,所以先停个一晚上,写个控制台下的计算器,来练练栈的使用. 首先,分析一下要完成的东西.输入肯定使用string类来输入的,然后第一步,就是把string的字 ...
- QT在Windows控制台下输出
原地址:http://blog.csdn.net/fjb2080/article/details/9013047 在windows的控制台下输出,需要在pro文件中加入: CONFIG += cons ...
- Windows控制台下绘制简单图形
最近接触到一个很有意思的问题,如何在Windows控制台下画图,翻遍了C的头文件也没找到画图的函数,好吧,那就用Windows提供的API函数吧,看来想移植是没戏了.先画一个简单的图,类似心电图那种吧 ...
- C#控制台下测试多线程的源码
下边代码是关于C#控制台下测试多线程的的代码,应该是对小伙伴有所用. class Program { static void Main(string[] args) { ThreadStart num ...
- 网络基础 Windows控制台下Ftp使用简介
Windows控制台下Ftp使用简介 by:授客 QQ:1033553122 测试环境: ftp服务器所在主机ip:172.25.75.2 ftp用户目录:F:\ftp C:\Users\laif ...
- .net core 中使用httpclient,HttpClientFactory的问题
Microsoft 在.Net Framework 4.5中引入了HttpClient,并且是在.NET服务器端代码中使用Web API的最常用方法.但它有一些严重的问题,如释放HttpClient对 ...
- Linux在终端和控制台下复制粘贴命令快捷键
1.在终端下: (1)复制命令:Ctrl + Shift + C 组合键. (2)粘贴命令:Ctrl + Shift + V 组合键. 2.在控制台下:(即vi编辑过程中) (1)复制命令:Ctrl ...
随机推荐
- 【NOJ2024】入栈序列和出栈序列
入栈序列和出栈序列 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 总提交:293 测试通过:68 比赛描述 给出入栈序列 ...
- git出现“The file will have its original line endings in your working directory”错误
一.现象: git add *时出现如下现象: The file will have its original line endings in your working directory 解决: G ...
- 关于background-image调整大小和位置的方法笔记
遇到background-image的问题有点多,直接上网搜资料自己整理一下 <!DOCTYPE html> <html lang="en"> <he ...
- js获取时间差值
function GetTime(firstDate, secondDate) { // 1.对事件进行处理 var firsttime = Date.parse(firstDate + " ...
- 如何把一个普通的Eclipse项目改造成Eclipse Plugin项目
New Project->Plug-in from existing JAR Archive 同时要记得不仅要将你要转换的项目的jar包选上,同时还要将项目依赖的jar包全部选上(要不然会找不到 ...
- 如何建一个Liferay 7的theme
首先附上原文链接Creating theme and Deploying in liferay 7 by using Eclipse 1.第一步:建一个Liferay module 项目,选择them ...
- SPSS详细操作:生存资料的Cox回归分析
SPSS详细操作:生存资料的Cox回归分析 一.问题与数据 某研究者拟观察某新药的抗肿瘤效果,将70名肺癌患者随机分为两组,分别采用该新药和常规药物进行治疗,观察两组肺癌患者的生存情况,共随访2年.研 ...
- database homework3
查询所有大于60分的学生的姓名和学号 (DISTINCT: 去重) mysql> select student.sname,student.sid,score.number from stude ...
- 洛谷2050 BZOJ2897美食节题解
放个链接 BZ链接 其实这题就是修车的加强版,做法差不多,还是对于每个厨师进行拆点 可是这样强行建图跑网络流会T飞 我们发现,如果一个厨师没有做倒数第x到菜,他一定不会做倒数第x+1到菜 我们的每次增 ...
- SQL Server 记录(更新中...)
sys.databases 显示所有数据库信息 sys.tables 显示当前数据库所有的表的信息 Go 向 SQL Server 实用工具发出一批 Transact-SQL 语句已结束的信号,Go本 ...