From fb558a85a26ba7bb7366c85db81196c72638c588 Mon Sep 17 00:00:00 2001 From: wanggaofeng <15601716045@163.com> Date: Fri, 5 Jan 2024 15:47:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HttpClientStudy.Core.csproj | 4 ++ HttpClientStudy.UnitTest/HttpClientTest.cs | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 HttpClientStudy.UnitTest/HttpClientTest.cs diff --git a/HttpClientStudy.Core/HttpClientStudy.Core.csproj b/HttpClientStudy.Core/HttpClientStudy.Core.csproj index 0ccbef8..fecc597 100644 --- a/HttpClientStudy.Core/HttpClientStudy.Core.csproj +++ b/HttpClientStudy.Core/HttpClientStudy.Core.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/HttpClientStudy.UnitTest/HttpClientTest.cs b/HttpClientStudy.UnitTest/HttpClientTest.cs new file mode 100644 index 0000000..54aa7ae --- /dev/null +++ b/HttpClientStudy.UnitTest/HttpClientTest.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Json; +using System.Text; +using System.Threading.Tasks; + +namespace HttpClientStudy.UnitTest +{ + public class HttpClientTest + { + private readonly ITestOutputHelper _logger; + public HttpClientTest(ITestOutputHelper outputHelper) + { + _logger = outputHelper; + } + + /// + /// Get请求中使用请求体 + /// 注意:服务器要设置(配置KestrelServerOptions AllowSynchronousIO值为true) + /// + /// + [Fact] + public async Task GetWithBodyTestAsync() + { + HttpClient httpClient = new HttpClient(); + + + var formData = new List> + { + new KeyValuePair("param1", "value1"), + new KeyValuePair("param2", "value2") + }; + + HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post,"http://localhost:5189/api/AdvancedGet/GetWithBody"); + + requestMessage.Content = new FormUrlEncodedContent(formData); + + + var response = await httpClient.SendAsync(requestMessage); + response.EnsureSuccessStatusCode(); + + } + } +}