Fetch_dotNET.Core
1.0.1
dotnet add package Fetch_dotNET.Core --version 1.0.1
NuGet\Install-Package Fetch_dotNET.Core -Version 1.0.1
<PackageReference Include="Fetch_dotNET.Core" Version="1.0.1" />
<PackageVersion Include="Fetch_dotNET.Core" Version="1.0.1" />
<PackageReference Include="Fetch_dotNET.Core" />
paket add Fetch_dotNET.Core --version 1.0.1
#r "nuget: Fetch_dotNET.Core, 1.0.1"
#:package Fetch_dotNET.Core@1.0.1
#addin nuget:?package=Fetch_dotNET.Core&version=1.0.1
#tool nuget:?package=Fetch_dotNET.Core&version=1.0.1
Fetch_dotNET.Core (Modern .NET)
Fetch_dotNET.Core è una libreria leggera di utility HTTP pensata per .NET moderno (net10.0).
Offre un wrapper ad alto livello su System.Net.Http.HttpClient con API sync/async, download file e utility di rete.
Nota: questa libreria espone una classe statica chiamata
HttpClient(namespaceFetch_dotNET.Core).
Per evitare confusione conSystem.Net.Http.HttpClient, nel tuo codice puoi usare l’alias del namespace oppure il nome completo.
Requisiti
- .NET 10.0
- Dipendenze:
Newtonsoft.Json(per parsing di opzioni/headers in JSON)
Installazione
.NET CLI
dotnet add package Fetch_dotNET.Core
Package Manager
Install-Package Fetch_dotNET.Core
Panoramica delle funzionalità
- GET / POST / PUT / DELETE (sync e async)
- Timeout per richiesta
- Headers personalizzati (User-Agent, Authorization, Content-Type, ecc.)
- Supporto a:
- Basic Auth
- Bearer JWT
- Invio dati:
- stringa (tipicamente JSON)
NameValueCollection(form-url-encoded)
- Download file
- Utility: IP locale/pubblico, MAC address, ping, check URL, ecc.
Quick start
using System;
using System.Threading.Tasks;
using Fetch_dotNET.Core;
class Program
{
static async Task Main()
{
// Verifica connessione
if (!HttpClient.IsConnectedToInternet())
return;
// GET (sync)
var r1 = HttpClient.Fetch("http://localhost/api/Azienda?lic_id=5383");
Console.WriteLine(r1);
// GET (async)
var r2 = await HttpClient.FetchAsync("http://localhost/api/Azienda?lic_id=5383");
Console.WriteLine(r2);
}
}
Esempi completi
Gli esempi seguenti sono presi dal progetto di esempio e mostrano l’uso delle principali API.
1) Utility di rete
bool isConnected = HttpClient.IsConnectedToInternet();
string myLocalIP = HttpClient.GetLocalIP();
string myPublicIP = HttpClient.GetPublicIP();
string myMAC = HttpClient.GetMacAddress();
2) Ping
var httpPingResponse = HttpClient.PingHost("http://192.168.1.1");
Console.WriteLine(httpPingResponse);
3) GET con timeout
var httpResponse = HttpClient.Fetch("http://localhost/api/Azienda?lic_id=5383", 3000);
Console.WriteLine(httpResponse);
4) GET async
var httpResponseAsync = await HttpClient.FetchAsync("http://localhost/api/Azienda?lic_id=5383");
Console.WriteLine(httpResponseAsync);
5) GET con Basic Auth (headers)
using System.Net;
using System.Text;
WebHeaderCollection headers = new WebHeaderCollection();
headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:password"));
headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
var resp = HttpClient.Fetch("http://localhost/api/Azienda?lic_id=5383", "GET", "", "", false, 3000, headers);
Console.WriteLine(resp);
6) GET con Bearer JWT (headers)
using System.Net;
WebHeaderCollection headers = new WebHeaderCollection();
headers[HttpRequestHeader.Authorization] = "Bearer " + jwt;
var resp = HttpClient.Fetch("http://localhost/api/Azienda?lic_id=5383", headers);
Console.WriteLine(resp);
7) GET con fetchOptions (JSON headers + timeout)
string fetchOptions = "{" +
"'method': 'GET'," +
"'headers': {" +
"'Accept': 'application/json'," +
"'Content-Type': 'application/json'," +
"'Authorization': 'Bearer " + jwt + "'," +
"'User-Agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0;)'" +
"}," +
"'timeout': 3000" +
"}";
var resp = await HttpClient.FetchAsync("http://localhost/api/Azienda?lic_id=5383", fetchOptions, data: null);
Console.WriteLine(resp);
8) POST JSON
string addressPost = "http://localhost/api/Appuntamenti";
string json = "{'shop_id':8000,'read':'secondary','events':[{'app_id':0,'eventType':'draft','eventColor':'lime'}]}";
var resp = HttpClient.Fetch(addressPost, "POST", json);
Console.WriteLine(resp);
9) POST con NameValueCollection (form)
using System.Collections.Specialized;
var values = new NameValueCollection();
values["product"] = "Fetch_dotNET";
values["version"] = "1.0.0";
values["log"] = "13/06/20 18:51:03 ERROR ParseFormUPDATE";
string addressLog = "http://localhost/api/addLog.php";
var resp = HttpClient.Fetch(addressLog, values);
Console.WriteLine(resp);
10) Download file
string fileUrl = "http://localhost/books/Extjs-Release1.0.0.pdf";
string fileName = @"C:\path\to\Extjs-Release1.0.0.pdf";
var resp = HttpClient.FetchDownloadFile(fileUrl, fileName);
Console.WriteLine(resp);
Note su timeout
Molti overload accettano timeout in millisecondi (es: 3000 = 3 secondi).
Licenza
Vedi la licenza del pacchetto su NuGet.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Newtonsoft.Json (>= 13.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
HTTP client helper library for modern .NET