main
wanggaofeng 1 year ago
parent 89047ab394
commit fb558a85a2

@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HttpClientStudy.Model\HttpClientStudy.Model.csproj" />
</ItemGroup>

@ -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;
}
/// <summary>
/// Get请求中使用请求体
/// 注意:服务器要设置(配置KestrelServerOptions AllowSynchronousIO值为true)
/// </summary>
/// <returns></returns>
[Fact]
public async Task GetWithBodyTestAsync()
{
HttpClient httpClient = new HttpClient();
var formData = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("param1", "value1"),
new KeyValuePair<string, string>("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();
}
}
}
Loading…
Cancel
Save