You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.5 KiB
C#

1 year ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
1 year ago
namespace HttpClientStudy.UnitTest.HttpClients
1 year ago
{
public class PipelineClientTest
{
1 year ago
private readonly ITestOutputHelper _logger;
1 year ago
1 year ago
public PipelineClientTest(ITestOutputHelper outputHelper)
{
_logger = outputHelper;
}
/// <summary>
/// 手动处理
/// </summary>
1 year ago
[Fact]
1 year ago
public async Task Delegating_Test()
1 year ago
{
1 year ago
//构建管道
11 months ago
var handler = new CustomHeadersHandler()
1 year ago
{
//相当于下一个中间件(管道)
11 months ago
InnerHandler = new SocketsHttpHandler()
{
AllowAutoRedirect = true
}
1 year ago
};
1 year ago
1 year ago
//构造中传入管道对象
HttpClient httpClient = new HttpClient(handler);
1 year ago
var sd = await httpClient.GetAsync(WebApiConfigManager.GetWebApiConfig().BaseUrl + "/api/health");
1 year ago
var contentText = await sd.Content.ReadAsStringAsync();
1 year ago
1 year ago
_logger.WriteLine(contentText);
}
[Fact]
public async Task CreateHttpClient_Test()
{
HttpClient client = new PipelineHttpClient().CreateHttpClient();
1 year ago
1 year ago
var r = await client.GetAsync(WebApiConfigManager.GetWebApiConfig().BaseUrl + "/api/health");
1 year ago
r.EnsureSuccessStatusCode();
1 year ago
}
}
}