当前位置:   article > 正文

.net core基于HttpClient实现的网络请求库_netcore httpclient封装

netcore httpclient封装

Soda.Http

基于HttpClient封装的 Http 请求库如果有什么好想法,可以提 Issue 或者 Pr。,如果想要使用,直接在nuget搜索Soda.Http即可。

Github项目地址:Soda.Http

用法

1 预载

预载配置并不是必须的,但是有助于我们进行一些通用基础设置,例如Headers、Accept、BaseUrl等等。

配置BaseUrl之后,可以直接使用QSodaHttp.Uri()代替QSodaHttp.Url(),直接填写接口即可。

AspNetCore中:

  1. services.AddSodaHttp(opts =>
  2. {
  3. opts.BaseUrl = "http://localhost:8080/";
  4. opts.Accept = new[]
  5. {
  6. "application/json",
  7. "text/plain",
  8. "*/*"
  9. };
  10. opts.EnableCompress = false;
  11. opts.Headers = new[]{
  12. ("X-Ca-Test", "key")
  13. };
  14. });

较为通用的写法,程序构建时:

  1. QSodaHttp.AddSodaHttp(opts =>
  2. {
  3. opts.BaseUrl = "http://localhost:8080/";
  4. opts.Accept = new[]
  5. {
  6. "application/json",
  7. "text/plain",
  8. "*/*"
  9. };
  10. opts.EnableCompress = false;
  11. opts.Headers = new[]{
  12. ("X-Ca-Test", "key")
  13. };
  14. })

2 全局配置 Authentication

有时需要全局配置 Authentication,如果在代码中请求中独立配置了 Authentication 则会覆盖全局 Authentication

QSodaHttp.InitAuthentication("Bearer", "Values");

如果你是塞到 Header 里的这种做法

QSodaHttp.AddHeader("X-Ca-Key", "Values");

3 Http 请求

3.1 QSodaHttp

API 示例:

  1. var result = await QSodaHttp.Url("https://www.baidu.com/")
  2. .Header("X-Ca-Key", "XXX")
  3. .Authentication("Bearer", "XXX")
  4. .Params(new { Id = "123456" })
  5. .Body(new { })
  6. // .Form(...)
  7. // .File(...)
  8. .PostAsync<string>();

简单示例:

  1. // 配置BaseUrl
  2. var services = new ServiceCollection();
  3. services.AddSodaHttp(opts =>
  4. {
  5. opts.EnableCompress = false;
  6. opts.BaseUrl = "http://localhost:5050/";
  7. });
var res = await QSodaHttp.Uri("/Test/Get").Params(new { Id = "123456" }).GetAsync<object>();
var res = await QSodaHttp.Uri("/Test/TestGetResult").Params(new { Id = "123456", Ids = new[] { "123", "456" } }).GetAsync<object>();
var res = await QSodaHttp.Uri("/Test/Post").Body(new { Id = "123456", Ids = new[] { "123", "456" } }).PostAsync<object>();
  1. var res = await QSodaHttp.Uri("/Test/PostResult")
  2. .Params(new { Id = "123456", Ids = new[] { "123", "456" } })
  3. .Body(new { Id = "123456", Ids = new[] { "123", "456" } })
  4. .PostAsync<object>();
var res = await QSodaHttp.Uri("/Test/Delete").Params(new { Id = "123456" }).DeleteAsync<object>();
var res = await QSodaHttp.Uri("/Test/DeleteResult").Params(new { Id = "123456", Ids = new[] { "123", "456" } }).DeleteAsync<object>();
var res = await QSodaHttp.Uri("/Test/Put").Params(new { Id = "123456" }).PutAsync<object>();
  1. var res = await QSodaHttp.Uri("/Test/PutResult")
  2. .Body(new { Id = "123456", Ids = new[] { "123", "456" } })
  3. .PutAsync<object>();
var res = await QSodaHttp.Uri("Patch").Params(new { Id = "123456" }).PatchAsync<object>();
  1. var res = await QSodaHttp.Uri("PatchResult")
  2. .Body(new { Id = "123456", Ids = new[] { "123", "456" } })
  3. .PatchAsync<object>();
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/259358?site
推荐阅读
相关标签
  

闽ICP备14008679号