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.
75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
using HttpClientStudy.Core.Utilities;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace HttpClientStudy.UnitTest
|
|
{
|
|
/// <summary>
|
|
/// 临时测试
|
|
/// </summary>
|
|
public class TempTest
|
|
{
|
|
private readonly ITestOutputHelper _output;
|
|
public TempTest(ITestOutputHelper output)
|
|
{
|
|
_output = output;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TestAsync()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddKeyedSingleton<HttpClient>("HttpClientA",new HttpClient()
|
|
{
|
|
//BaseAddress = new Uri("https://localhost:5001/"),
|
|
Timeout = TimeSpan.FromSeconds(10),
|
|
});
|
|
|
|
var client = services.BuildServiceProvider().GetRequiredKeyedService<HttpClient>("HttpClientA");
|
|
|
|
var resp = await client.GetAsync("https://www.baidu.com");
|
|
resp.EnsureSuccessStatusCode();
|
|
|
|
var content = await resp.Content.ReadAsStringAsync();
|
|
Console.WriteLine(content.Length);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Temp_TestAsync()
|
|
{
|
|
var services = new ServiceCollection();
|
|
//services.AddHttpClient();
|
|
services
|
|
.AddHttpClient<HttpClient>(config =>
|
|
{
|
|
config.BaseAddress = new Uri("https://www.baidu.com");
|
|
})
|
|
.ConfigureHttpClient(config =>
|
|
{
|
|
config.BaseAddress = new Uri("https://www.qq.com");
|
|
});
|
|
|
|
|
|
|
|
var provider = services.BuildServiceProvider();
|
|
|
|
var factory = provider.GetRequiredService<IHttpClientFactory>();
|
|
|
|
var client = factory.CreateClient();
|
|
|
|
var res = await client.GetAsync("https://www.qq.com");
|
|
res.EnsureSuccessStatusCode();
|
|
var content = await res.Content.ReadAsStringAsync();
|
|
|
|
var sum = content.Length;
|
|
_output.WriteLine(sum.ToString());
|
|
}
|
|
}
|
|
}
|