0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MiniAPI(二十二):HttpClient

Posted at

説明:この記事ではHttpClientの使用方法ではなく、asp.net core mini apiフレームワークでのHttpClientの導入と使用方法について共有します。
ビジネス開発では、第三者のサービスを呼び出すことが避けられず、HttpClientが使用されます。早期のasp.net coreフレームワークでは、新しいHttpClientを通じて第三者へのリクエストを実現していましたが、現在ではHttPClientFactoryを通じて実現できます。これにより、接続をプールしてリソースを節約できます。
基本的な使用方法は非常に簡単です:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient();
var app = builder.Build();
app.MapGet("/test", async (IHttpClientFactory clientFactory) => {
    var client = clientFactory.CreateClient();
    var content = await client.GetStringAsync("https://www.google.com");
});
app.Run();

プロジェクトに複数の第三者サービスリクエストがある場合、各サービスを区別するために、命名方式を採用できます

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient("Google", httpClient =>{
    httpClient.BaseAddress = new Uri("https://www.google.com/");
});
builder.Services.AddHttpClient("BaiDu", httpClient =>{
    httpClient.BaseAddress = new Uri("https://www.baidu.com/");
});
var app = builder.Build();
app.MapGet("/testgoogle", async (IHttpClientFactory clientFactory) => {
    var googleClient = clientFactory.CreateClient("Google");
    return await googleClient.GetStringAsync("search?q=桂素伟");
});
app.MapGet("/testbaidu", async (IHttpClientFactory clientFactory) => {
    var baiduClient = clientFactory.CreateClient("BaiDu");
    return await baiduClient.GetStringAsync("s?wd=桂素伟");
});
app.Run();

また、プロジェクト内の各サービスのリクエストをそれぞれに封印し、それぞれのHttpClientを使用することもできます:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient<IGoogleService, GoogleService>();
builder.Services.AddHttpClient<IBaiDuService, BaiDuService>(httpClient =>{
    httpClient.BaseAddress = new Uri("https://www.baidu.com/");
});
var app = builder.Build();
app.MapGet("/testgoogle", async (IGoogleService google) => {
    return await google.GetContentAsync();
});
app.MapGet("/testbaidu", async (IBaiDuService baidu) => {
    return await baidu.GetContentAsync();
});
app.Run();
interface IGoogleService{
    Task<string> GetContentAsync();
}
class GoogleService : IGoogleService{
    private readonly HttpClient _httpClient;
    public GoogleService(HttpClient httpClient)
    {
        _httpClient = httpClient;
        _httpClient.BaseAddress = new Uri("https://www.google.com/");
    }
    public async Task<string> GetContentAsync()
    {
        return await _httpClient.GetStringAsync("search?q=桂素伟");
    }
}
interface IBaiDuService{
    Task<string> GetContentAsync();
}
class BaiDuService : IBaiDuService{
    private readonly HttpClient _httpClient;
    public BaiDuService(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }
    public async Task<string> GetContentAsync()
    {
        return await _httpClient.GetStringAsync("s?wd=桂素伟");
    }
}

(Translated by GPT)

元のリンク:https://mp.weixin.qq.com/s?__biz=MzA3NDM1MzIyMQ==&mid=2247484977&idx=1&sn=4023f8c03b848e23d547dd3b51433036&chksm=9f00591ba877d00dc9061a553ce7d09396d59d3050ab900ec0ec690818667bd248eeeacb0906&token=541757603&lang=zh_CN#rd

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?