.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 ...
随机推荐
- POJ1182 NOI2001 食物链
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 77428 Accepted: 23067 Description ...
- OpenLayers图层
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...
- java路径中'/'的使用
考虑java的跨系统:uinux和winw7中的‘/'标识方法不同,使用下放语句可避免 File.separator;//代表"/"
- 关于Python缩进,我们该了解哪些?
Python是一门独特的语言,它的代码块是通过缩进(Indentation)来标记的(大部分语言都是使用花括号作为代码块的标记),具有相同缩进的多行代码属于同一个代码块.如果代码莫名其妙的乱缩进,Py ...
- Leetcode687.Longest Univalue Path最长同值路径
给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值. 这条路径可以经过也可以不经过根节点. 注意:两个节点之间的路径长度由它们之间的边数表示. 示例 1: 输入: 5 / \ 4 5 / ...
- 详解php中空字符串和0之间的关系
$_x=$row["x"];$_y=$row["y"];if(isset($_x) && isset($_y)){if($row["y ...
- 【风马一族_php】NO5_php基础知识_数组
原文来自:http://www.cnblogs.com/sows/p/6032570.html (博客园的)风马一族 侵犯版本,后果自负 回顾 匿名函数 定义: 变量 = function [参数列表 ...
- jQuery圆盘抽奖
在线演示 本地下载
- objectarx之遍历当前模型空间中的所有实体,并对每个实体进行炸开
//炸开void BomEntity(){ AcDbBlockTable *pBlkTbl; acdbHostApplicationServices()->workingDatabase()-& ...
- MUI - 打开页面默认弹出键盘及返回关闭键盘
打开页面默认弹出键盘及返回关闭键盘 http://www.cnblogs.com/phillyx/ (function(keyboard) { var openSoftKeyboard = funct ...