From f907d5bc6a796daa5d5bb7fb383262351da9c789 Mon Sep 17 00:00:00 2001 From: wanggaofeng <15601716045@163.com> Date: Thu, 4 Jan 2024 20:55:22 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpClientStudy.Core/Class1.cs | 7 -- HttpClientStudy.Core/SimpleHttpClient.cs | 102 ++++++++++++++++++ .../Core/SimpleHttpClientTest.cs | 48 +++++++++ .../.config/dotnet-tools.json | 12 +++ HttpClientStudy.WebApp/BaseResult.cs | 12 ++- 5 files changed, 172 insertions(+), 9 deletions(-) delete mode 100644 HttpClientStudy.Core/Class1.cs create mode 100644 HttpClientStudy.Core/SimpleHttpClient.cs create mode 100644 HttpClientStudy.UnitTest/Core/SimpleHttpClientTest.cs create mode 100644 HttpClientStudy.WebApp/.config/dotnet-tools.json diff --git a/HttpClientStudy.Core/Class1.cs b/HttpClientStudy.Core/Class1.cs deleted file mode 100644 index 5fc3f40..0000000 --- a/HttpClientStudy.Core/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace HttpClientStudy.Core -{ - public class Class1 - { - - } -} diff --git a/HttpClientStudy.Core/SimpleHttpClient.cs b/HttpClientStudy.Core/SimpleHttpClient.cs new file mode 100644 index 0000000..30336fd --- /dev/null +++ b/HttpClientStudy.Core/SimpleHttpClient.cs @@ -0,0 +1,102 @@ +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Net.Http; +using System.Net.Mime; +using System.Net; + +namespace HttpClientStudy.Core +{ + /// + /// 简单 HttpClient 包装类 + /// + public class SimpleHttpClient + { + public string Get(string url) + { + HttpClient client = new HttpClient(); + //可以统一设置HttpClient属性:HttpClient可以使用统一设置进行多次请求。 + //client.BaseAddress = new Uri(url); + //client.DefaultRequestHeaders.Add("Accept", "application/json"); + //client.DefaultRequestVersion = HttpVersion.Version10; + //client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher; + //client.Timeout = TimeSpan.FromSeconds(60); + //client.CancelPendingRequests(); + + //只有Send是同步方法,其它全为异步方法。 + + //Send同步方法,需要HttpRequestMessage参数 + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url); + var response = client.Send(request); + + response.EnsureSuccessStatusCode(); + //或者 + if (response.IsSuccessStatusCode) + { + //业务逻辑 + } + + //状态码 + if (response is { StatusCode: HttpStatusCode.OK }) + { + //业务逻辑 + } + + //请求头 + if(response is { Headers.ETag:null}) + { + //业务逻辑 + } + + var result = response.Content.ReadAsStringAsync().Result; + + return result; + } + + public async Task GetAsync(string url) + { + HttpClient client = new HttpClient(); + var response = await client.GetAsync(url); + + response.EnsureSuccessStatusCode(); + + var result = await response.Content.ReadAsStringAsync(); + + return result; + } + + public TResult? GetJson(string url) + { + HttpClient client = new HttpClient(); + + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url); + var response = client.Send(request); + + response.EnsureSuccessStatusCode(); + if (response.Content.Headers.ContentType?.MediaType != MediaTypeNames.Application.Json) + { + throw new HttpRequestException("响应不是 json 格式!"); + } + + var result = response.Content.ReadFromJsonAsync().Result; + + return result; + } + + public async Task GetJsonAsync(string url) + { + HttpClient client = new HttpClient(); + + var response = await client.GetAsync(url); + + response.EnsureSuccessStatusCode(); + if (response.Content.Headers.ContentType?.MediaType != MediaTypeNames.Application.Json) + { + throw new HttpRequestException("响应不是 json 格式!"); + } + + var result = await response.Content.ReadFromJsonAsync(); + + return result; + } + } +} diff --git a/HttpClientStudy.UnitTest/Core/SimpleHttpClientTest.cs b/HttpClientStudy.UnitTest/Core/SimpleHttpClientTest.cs new file mode 100644 index 0000000..20f270c --- /dev/null +++ b/HttpClientStudy.UnitTest/Core/SimpleHttpClientTest.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HttpClientStudy.UnitTest.Core +{ + /// + /// SimpleHttpClient 测试类 + /// + public class SimpleHttpClientTest + { + private readonly ITestOutputHelper _logger; + public SimpleHttpClientTest(ITestOutputHelper outputHelper) + { + _logger = outputHelper; + } + + [Fact] + public void Get_Test() + { + SimpleHttpClient client = new SimpleHttpClient(); + + var result = client.Get("http://localhost:5000/api/Simple/GetAccount"); + + Assert.NotNull(result); + Assert.NotEmpty(result); + } + + [Fact] + public void Test() + { + SimpleHttpClient client = new SimpleHttpClient(); + + var result = client.GetJson>("http://localhost:5000/api/Simple/GetAccount"); + + Assert.NotNull(result); + Assert.IsAssignableFrom(result); + Assert.Equal(1, result.Code); + Assert.Contains("成功", result.Message); + Assert.IsType(result.Message); + Assert.NotEmpty(result.Message); + } + + + } +} diff --git a/HttpClientStudy.WebApp/.config/dotnet-tools.json b/HttpClientStudy.WebApp/.config/dotnet-tools.json new file mode 100644 index 0000000..e3cadb9 --- /dev/null +++ b/HttpClientStudy.WebApp/.config/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "8.0.0", + "commands": [ + "dotnet-ef" + ] + } + } +} \ No newline at end of file diff --git a/HttpClientStudy.WebApp/BaseResult.cs b/HttpClientStudy.WebApp/BaseResult.cs index 0fe2df0..1407778 100644 --- a/HttpClientStudy.WebApp/BaseResult.cs +++ b/HttpClientStudy.WebApp/BaseResult.cs @@ -12,7 +12,6 @@ namespace HttpClientStudy.WebApp /// public int Code { get; set; } - /// /// /// 接口返回说明信息 /// @@ -30,11 +29,21 @@ namespace HttpClientStudy.WebApp /// public class BaseResult : BaseResult { + /// + /// 创建返回基数 + /// + /// 泛型数据 + /// 编码 + /// 说明信息 + /// public static BaseResult Create(T? data = default, int code = 0, string message = "") { return new BaseResult { Data = data, Code = code, Message = message }; } + /// + /// 泛型返回核心数据 + /// public new T? Data { get; set; } } @@ -154,7 +163,6 @@ namespace HttpClientStudy.WebApp /// /// 创建 泛型异常返回基类 /// - public static BaseResult Exception(Exception ex, TData? data, int code = 0) { return new BaseResult() { Data = data, Code = code, Message = $"异常:{ex.Message}" };