1 Star 1 Fork 0

Auser/HttpClientLite

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

HttpClientLite

介绍

HttpClientLite 是一个基于 .NET 的高可扩展 HTTP 客户端管理库,提供了对 HTTP 请求的全面管理,包括:

  • 请求/响应配置
  • 异常处理
  • 支持同步与异步操作
  • 丰富的扩展方法,支持 GET、POST、PUT 等操作
  • 支持 FormData 和 JSON 数据传输

特性

  1. 高度模块化:通过配置和处理器轻松扩展功能。
  2. 请求克隆:支持对 HttpRequestMessage 的克隆,便于重试或日志处理。
  3. 扩展方法:提供常用 HTTP 请求的简化方法。
  4. 异常管理:支持自定义异常处理逻辑。

软件架构

支持net4.6.2、net4.7.2、net4.8.1、net6、net8、net9

安装教程

dotnet add package HttpClientLite.Extensions

使用说明

using HttpClientLite;
using HttpClientLite.Extensions;
using HttpClientLite.Handlers.interfaces;
using HttpClientLite.Configurations.interfaces;

var httpManager = new HttpManager("https://api.example.com");

发送GET请求

var response = await httpManager.GetAsync("/endpoint");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);

发送 POST 请求 (JSON 数据)

var requestBody = new { key = "value" };
var response = await httpManager.PostJsonAsync("/endpoint", requestBody);

添加异常处理

httpManager.HttpManagerExceptionHandler((exception, response) =>
{
    Console.WriteLine($"Error: {exception.Message}");
});

配置与扩展

配置 HttpManager

httpManager.HttpManagerConfiguration(manager =>
{
    manager.BaseAddress = "https://api.customdomain.com";
});

或者 自定义类继承并实现 HttpClientLite.Configurations.interfaces.IHttpManagerConfiguration

httpManager.Configuration(new CustomHttpManagerConfiguration());

配置 HttpClient

httpManager.HttpClientConfiguration(client =>
{
    client.Timeout = TimeSpan.FromSeconds(30);
    client.DefaultRequestHeaders.Add("Authorization", "Bearer token");
});

或者 自定义类继承并实现 HttpClientLite.Configurations.interfaces.IHttpClientConfiguration

httpManager.Configuration(new CustomHttpClientConfiguration());

配置 HttpRequestMessage

httpManager.RequestMessageConfiguration(request =>
{
    request.Headers.Add("Custom-Header", "CustomValue");
});

或者 自定义类继承并实现 HttpClientLite.Configurations.interfaces.IRequestMessageConfiguration

httpManager.Configuration(new CustomRequestMessageConfiguration());

自定义响应处理

httpManager.ResponseMessageHandler((manager, client, request, response, token) =>
{
    if (!response.IsSuccessStatusCode)
    {
        Console.WriteLine($"Request failed with status code {response.StatusCode}");
    }
    return response;
});

或者 自定义类继承并实现HttpClientLite.Handlers.interfaces.IResponseMessageHandler

httpManager.Handler(new CustomResponseMessageHandler());

发送文件

var content = new MultipartFormDataContent();
content.AddFile("file", "example.txt", File.ReadAllBytes("example.txt"));
var request = new HttpRequestMessage(HttpMethod.Post, "/upload") { Content = content };
var response = await httpManager.SendAsync(request);

或者

Stream stream = File.OpenRead("example.txt");
content.AddFile("file", "example.txt", stream);

扩展方法一览

目前实现比较常用的:

HttpClientLite.Extensions.HttpManagerExtensions

方法 描述
GetAsync 异步发送 GET 请求
PostJsonAsync 异步发送 POST JSON 请求
PostFormDataAsync 异步发送 POST FormData 请求
PutJsonAsync 异步发送 PUT JSON 请求
PutFormDataAsync 异步发送 PUT FormData 请求

示例

综合示例

using System.Web;
using System.Net.Http;
using HttpClientLite;
using HttpClientLite.Extensions;
using HttpClientLite.Handlers.interfaces;
using HttpClientLite.Configurations.interfaces;

var httpManager = new HttpManager("https://api.example.com");

// 配置 HttpClient
httpManager.HttpClientConfiguration(client =>
{
    client.DefaultRequestHeaders.Add("Authorization", "Bearer example-token");
});

// 配置 HttpRequestMessage
httpManager.RequestMessageConfiguration(request =>
{
    request.Headers.Add("Custom-Header", "HeaderValue");
});

// 添加异常处理
httpManager.HttpManagerExceptionHandler((exception, response) =>
{
    Console.WriteLine($"Request failed: {exception.Message}");
});

// 发起请求
var response = await httpManager.GetAsync("/users");
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
MIT License Copyright (c) 2024 Auser Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

HttpClientLite 是一个基于 .NET 的高可扩展 HTTP 客户端管理库,提供了对 HTTP 请求的全面管理,包括: 请求/响应配置 异常处理 支持同步与异步操作 丰富的扩展方法,支持 GET、POST、PUT 等操作 支持 FormData 和 JSON 数据传输 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/Ausername/HttpClientLite.git
git@gitee.com:Ausername/HttpClientLite.git
Ausername
HttpClientLite
HttpClientLite
master

搜索帮助