Cosmos.Network.Http
2.0.0
See the version list below for details.
dotnet add package Cosmos.Network.Http --version 2.0.0
NuGet\Install-Package Cosmos.Network.Http -Version 2.0.0
<PackageReference Include="Cosmos.Network.Http" Version="2.0.0" />
<PackageVersion Include="Cosmos.Network.Http" Version="2.0.0" />
<PackageReference Include="Cosmos.Network.Http" />
paket add Cosmos.Network.Http --version 2.0.0
#r "nuget: Cosmos.Network.Http, 2.0.0"
#:package Cosmos.Network.Http@2.0.0
#addin nuget:?package=Cosmos.Network.Http&version=2.0.0
#tool nuget:?package=Cosmos.Network.Http&version=2.0.0
<h1 align="center">CosmosHTTP Client 🚀</h1> <p> <a href="https://www.nuget.org/packages/Cosmos.Network.Http/" target="_blank"> <img alt="Version" src="https://img.shields.io/nuget/v/Cosmos.Network.Http.svg" /> </a> <a href="https://github.com/CosmosOS/Cosmos.Network.Http/blob/main/LICENSE.txt" target="_blank"> <img alt="License: BSD Clause 3 License" src="https://img.shields.io/badge/license-BSD License-yellow.svg" /> </a> </p>
CosmosHTTP is an HTTP/1.1 client made in C# for the Cosmos operating system construction kit.
Usage
Add the package to your kernel .csproj:
<ItemGroup>
<PackageReference Include="Cosmos.Network.Http" Version="2.0.0" />
</ItemGroup>
The kernel needs networking (CosmosEnableNetwork, on by default), an IP configuration (DHCP or static) and, for host names, a DNS server. Send() runs the request on the calling thread and returns the response once it has arrived whole:
using System;
using System.IO;
using Cosmos.Network.Http;
HttpResponse response = new HttpRequest("http://httpforever.com/").Send();
Console.WriteLine($"{response.StatusCode} {response.ReasonPhrase}, {response.Content.Length} bytes");
File.WriteAllBytes("/0/index.html", response.Content);
Send() returns error statuses too; EnsureSuccessStatusCode() turns them into an HttpException, and GetString() decodes the body with the charset of its Content-Type:
string json = new HttpRequest("http://example.com/data.json").Send().EnsureSuccessStatusCode().GetString();
A request can set its method, body, headers, timeout and how many redirects it follows:
HttpResponse response = new HttpRequest("http://example.com/api")
{
Method = "POST",
Body = Encoding.UTF8.GetBytes("{\"name\":\"cosmos\"}"),
Headers = { ["Content-Type"] = "application/json" },
Timeout = 10_000, // how long the server may stay silent, in milliseconds (15 s by default)
MaxRedirects = 0, // return redirects instead of following them (5 by default)
// Optional: one line per response and redirect.
Log = message => Cosmos.Kernel.System.Diagnostics.Log.WriteString(message + "\n"),
}.Send();
The Host header comes from the URL. Setting it in Headers reaches a virtual host by IP address:
new HttpRequest("http://34.223.124.45/") { Headers = { ["Host"] = "neverssl.com" } }.Send();
Limits
http://only: there is no TLS, sohttps://URLs, and redirects to them, throw.- Each request opens a connection of its own, and the server closes it once it has answered.
- Responses come without content coding (
Accept-Encoding: identity): a Cosmos kernel has no gzip to undo. - The whole body is held in memory.
Threads
Send() never waits in Thread.Sleep: it waits in Socket.Poll, which returns at once on a Cosmos kernel. So it runs on the kernel's main loop, which must never block, as well as on a thread of its own.
Authors
👤 @2881099 (the first version was inspired by TcpClientHttpRequest)
🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
📝 License
Copyright © 2023-2026 CosmosOS.
This project is BSD Clause 3 licensed.
| 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
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.