简体中文 | English
A modern, simple, and powerful HTTP client library for .NET.
- Simple API: Clean and intuitive interface for making HTTP requests
- Multiple HTTP Methods: Built-in support for GET, POST, PUT, DELETE
- Flexible Response Handling: Support for text and JSON deserialization
- Request/Response Interceptors: Hook into the request and response lifecycle
- Cross-platform: Supports .NET Standard 2.0 and .NET 5.0+
- Modern Design: Built with async/await patterns
Install the package via NuGet:
dotnet add package Delly.FetchingOr via the NuGet Package Manager:
Install-Package Delly.Fetching
using Delly.Fetching;
// Simple GET request
string html = await FetchUtils.GetAsync("https://api.example.com/data");
// Using builder pattern
string html = await FetchUtils.Create("https://api.example.com/data")
.Build()
.GetAsync();// Get and deserialize JSON response
var user = await FetchUtils.GetAsync<User>("https://api.example.com/user/1");using Delly.Fetching;
// POST request with JSON data
var loginData = new
{
userCode = "testuser",
password = "testpass"
};
var response = await FetchUtils.Create("https://api.example.com/login")
.AppendJsonData(loginData)
.PostAsync();
// POST request with JSON data and custom serialization options
var options = new JsonSerializerOptions {
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var response = await FetchUtils.Create("https://api.example.com/users")
.AppendJsonData(newUser, options)
.PostAsync();using Delly.Fetching;
// Simple POST request
string response = await FetchUtils.PostAsync("https://api.example.com/users");
// POST request with JSON response
var newUser = await FetchUtils.PostAsync<User>("https://api.example.com/users");using Delly.Fetching;
// Create a client with custom configuration
var client = FetchUtils.Create("https://api.example.com/data")
.Request(builder => {
// Customize request
builder.SetMethod(HttpMethod.Get);
builder.OnBuilding(request => {
request.Headers.Add("Authorization", "Bearer your-token");
});
})
.Build();
// Send request
string response = await client.GetAsync();// PUT request
string updated = await FetchUtils.PutAsync("https://api.example.com/users/1");
// DELETE request
string result = await FetchUtils.DeleteAsync("https://api.example.com/users/1");
// Using client
var client = FetchUtils.Create("https://api.example.com/users/1").Build();
await client.PutAsync();
await client.DeleteAsync();Send JSON data easily with the AppendJsonData extension method:
using Delly.Fetching;
// Simple JSON POST request
var userData = new
{
name = "John Doe",
email = "john@example.com"
};
var response = await FetchUtils.Create("https://api.example.com/users")
.AppendJsonData(userData)
.PostAsync();
// JSON POST with custom serialization options
var options = new JsonSerializerOptions {
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
var response = await FetchUtils.Create("https://api.example.com/users")
.AppendJsonData(userData, options)
.PostAsync();
// JSON POST with additional headers
var response = await FetchUtils.Create("https://api.example.com/users")
.Request(builder => {
builder.AppendJsonData(userData);
builder.OnBuilding(request => {
request.Headers.Add("Authorization", "Bearer token");
});
})
.PostAsync();var client = FetchUtils.Create("https://api.example.com/data")
.Build();
// Intercept and modify request before sending
client.OnRequesting(request => {
request.Headers.Add("Custom-Header", "value");
return request;
});
string response = await client.GetAsync();var client = FetchUtils.Create("https://api.example.com/data")
.Build();
// Intercept and modify response after receiving
client.OnResponsing(response => {
// Log response status
Console.WriteLine($"Status: {response.StatusCode}");
return response;
});
string response = await client.GetAsync();var builder = FetchUtils.Create("https://api.example.com/data");
// Set custom HTTP client handler
builder.SetHttpHandler(new HttpClientHandler {
AutomaticDecompression = DecompressionMethods.GZip,
AllowAutoRedirect = true,
UseProxy = false
});
var client = builder.Build();
string response = await client.GetAsync();var options = new JsonSerializerOptions {
PropertyNameCaseInsensitive = true,
WriteIndented = true
};
var client = FetchUtils.Create("https://api.example.com/data")
.Build();
// Get JSON with custom options
var data = await client.GetAsync<MyData>(options);| Method | Description |
|---|---|
Create(string url) |
Create a fetch builder with URL |
GetAsync(string url) |
GET request returning string |
GetAsync<T>(string url) |
GET request returning deserialized object |
PostAsync(string url) |
POST request returning string |
PostAsync<T>(string url) |
POST request returning deserialized object |
PutAsync(string url) |
PUT request returning string |
PutAsync<T>(string url) |
PUT request returning deserialized object |
DeleteAsync(string url) |
DELETE request returning string |
DeleteAsync<T>(string url) |
DELETE request returning deserialized object |
| Method | Description |
|---|---|
Request(Action<IFetchRequestBuilder>) |
Configure request builder |
AppendJsonData<T>(T data, JsonSerializerOptions) |
Add JSON data to request body |
| Method | Description |
|---|---|
AppendJsonData<T>(T data, JsonSerializerOptions) |
Add JSON data to request body |
| Method | Description |
|---|---|
GetAsync() |
GET request returning string |
GetAsync<T>(JsonSerializerOptions) |
GET request returning deserialized object |
PostAsync() |
POST request returning string |
PostAsync<T>(JsonSerializerOptions) |
POST request returning deserialized object |
PutAsync() |
PUT request returning string |
PutAsync<T>(JsonSerializerOptions) |
PUT request returning deserialized object |
DeleteAsync() |
DELETE request returning string |
DeleteAsync<T>(JsonSerializerOptions) |
DELETE request returning deserialized object |
SendAsync(HttpMethod) |
Send request with specified method |
OnRequesting(Func) |
Register request interceptor |
OnResponsing(Func) |
Register response interceptor |
# Clone the repository
git clone https://github.com/delly-net/Fetching.git
cd Fetching
# Restore dependencies
dotnet restore
# Build the solution
dotnet build
# Run tests
dotnet testContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
Thanks to all contributors who have helped make this project better.
Made with ❤️ by delly.net