1 Star 1 Fork 0

Auser/HttpClientLite

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
README.en.md 5.08 KB
一键复制 编辑 原始数据 按行查看 历史
Auser 提交于 7个月前 . 更新说明

HttpClientLite

Description

HttpClientLite is a.NET-based, highly scalable HTTP client management library that provides comprehensive management of HTTP requests, including:

  • Request/response configuration
  • Exception handling
  • Supports synchronous and asynchronous operations
  • Rich extension methods, support GET, POST, PUT and other operations
  • Supports FormData and JSON data transmission

Feature

  1. Highly modular : Easily expand functionality through configuration and processor.
  2. Request cloning : Support for 'HttpRequestMessage' cloning, easy to retry or log processing.
  3. Extended Method : Provides a simplified method of common HTTP requests.
  4. Exception Management : Supports custom exception handling logic.

Software Architecture

Supports net4.6.2, net4.7.2, net4.8.1, net6, net8, and net9

Installation

dotnet add package HttpClientLite.Extensions

Usage

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

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

Send GET request

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

Sending POST requests (JSON data)

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

Add exception handling

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

Configuration and extension

Configuration HttpManager

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

or Custom classes inherit and implement HttpClientLite.Configurations.interfaces.IHttpManagerConfiguration

httpManager.Configuration(new CustomHttpManagerConfiguration());

Configuration HttpClient

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

or Custom classes inherit and implement HttpClientLite.Configurations.interfaces.IHttpClientConfiguration

httpManager.Configuration(new CustomHttpClientConfiguration());

Configuration HttpRequestMessage

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

or Custom classes inherit and implement HttpClientLite.Configurations.interfaces.IRequestMessageConfiguration

httpManager.Configuration(new CustomRequestMessageConfiguration());

Customize response handling

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

or Custom classes inherit and implement HttpClientLite.Handlers.interfaces.IResponseMessageHandler

httpManager.Handler(new CustomResponseMessageHandler());

Send file

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);

or

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

List of extension methods

The current implementation is more commonly used:

HttpClientLite.Extensions.HttpManagerExtensions

Method Description
GetAsync Send GET requests asynchronously
PostJsonAsync Send the POST JSON request asynchronously
PostFormDataAsync The POST FormData request is sent asynchronously
PutJsonAsync Asynchronously send a PUT JSON request
PutFormDataAsync Send the PUT FormData request asynchronously

Example

Comprehensive example

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");

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

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

// Add exception handling
httpManager.HttpManagerExceptionHandler((exception, response) =>
{
    Console.WriteLine($"Request failed: {exception.Message}");
});

// Send request
var response = await httpManager.GetAsync("/users");
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);

Contribution

  1. Fork the repository
  2. Create Feat_xxx branch
  3. Commit your code
  4. Create Pull Request
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/Ausername/HttpClientLite.git
git@gitee.com:Ausername/HttpClientLite.git
Ausername
HttpClientLite
HttpClientLite
master

搜索帮助