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. 30分钟学webpack实战

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

  2. WordPress使用自定义文章类型实现任意模板的方法和怎么做邮件回复

    主要就是使用了register_post_type 函数. 1.创建插件目录 新建一个文件夹用来存放插件文件,这里我就命名这个文件夹为myMood 2.创php代码文件 在刚才创建的文件夹里面新建一个 ...

  3. SQLServer —— 流程控制语句

    一.IF - ELSE 语法: IF(条件) BEGIN 语句1 语句2 ... END ELSE BEGIN 语句1 语句2 ... END 说明: ELSE是可选部分,如果有多条语句,才需要BEG ...

  4. Docker for windows pull镜像文件的安装位置改变方法

    发生现象: 在windows10下安装docker for windows,随着用docker pull image文件后,C盘的容量越来越小了,你可能也有一种跟我一样的想法,想改变默认的安装路径,本 ...

  5. 10种简单的Java性能优化(转)

    本文由 ImportNew - 一直在路上 翻译自 jaxenter.欢迎加入翻译小组.转载请见文末要求. 你是否正打算优化hashCode()方法?是否想要绕开正则表达式?Lukas Eder介绍了 ...

  6. 2019-8-31-dotnet-方法名-To-和-As-有什么不同

    title author date CreateTime categories dotnet 方法名 To 和 As 有什么不同 lindexi 2019-08-31 16:55:58 +0800 2 ...

  7. Person Re-identification 系列论文笔记(八):SPReID

    Human Semantic Parsing for Person Re-identification Kalayeh M M, Basaran E, Gokmen M, et al. Human S ...

  8. icheck的使用

    一.什么是icheck 就是用来美化单选框.复选框的. 二.如何使用 1.下载 到 github 下载.https://github.com/fronteed/icheck 下载完毕.解压.目录结构如 ...

  9. Code Force 429B Working out【递推dp】

    Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the ...

  10. idea 使用优化

    1.创建类的模板 /** * Copyright (C), 2015-${YEAR}, XXX有限公司 * FileName: ${NAME} * Author: ${USER} * Date: ${ ...