nanoFramework.System.Net.Http.Server 1.5.150

Prefix Reserved
dotnet add package nanoFramework.System.Net.Http.Server --version 1.5.150                
NuGet\Install-Package nanoFramework.System.Net.Http.Server -Version 1.5.150                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nanoFramework.System.Net.Http.Server" Version="1.5.150" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.System.Net.Http.Server --version 1.5.150                
#r "nuget: nanoFramework.System.Net.Http.Server, 1.5.150"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install nanoFramework.System.Net.Http.Server as a Cake Addin
#addin nuget:?package=nanoFramework.System.Net.Http.Server&version=1.5.150

// Install nanoFramework.System.Net.Http.Server as a Cake Tool
#tool nuget:?package=nanoFramework.System.Net.Http.Server&version=1.5.150                

Quality Gate Status Reliability Rating License NuGet #yourfirstpr Discord

nanoFramework logo


Welcome to the .NET nanoFramework System.Net.Http Library repository

Build status

Component Build Status NuGet Package
System.Net.Http Build Status NuGet
System.Net.Http.Client Build Status NuGet
System.Net.Http.Server Build Status NuGet

Usage examples

The API, classes and namespaces in this library follow, as close as possible, the .NET ones. Exceptions are the lack of async calls, generics and the Task, async/await model. The names reflect that by dropping the Async suffix and not returning Task and the lack of the overloaded methods with CancelationToken parameters.

Also worth mentioning is the need to pass the CA root certificate to HttpClient in order to be able to validate the server certificate.

HttpClient calling REST services

HttpClient makes it very easy to connect and consume REST services. In order to use it, one has to create the object and them perform the calls. Note that HttpClient is meant to be reused throughout the application lifecycle. There is no need to create a new instance every time a call has to be performed. Like this:

static readonly HttpClient _httpClient = new HttpClient();

To pass the CA root certificate allowing to validate the secure server certificate. The CA root cert can also come from a binary file or text file from a resource.

_httpClient.HttpsAuthentCert = new X509Certificate(
@"-----BEGIN CERTIFICATE-----
MIIEDzCCAvegAwIBAgIBADANBgkqhkiG9w0BAQUFADBoMQswCQYDVQQGEwJVU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mH8lU+nLMPUxA2IGvd56D
eruix/U0F47ZEUD0/CwqTRV/p2JdLiXTAAsgGh1o+Re49L2L7ShZ3U0WixeDyLJl
xy16paq8U4Zt3VekyvggQQto8PT7dL5WXXp59fkdheMtlb71cZBDzI0fmgAKhynp
VSJYACPq4xJDKVtHCN2MQWplBqjlIapBtJUhlbl90TSrE9atvNziPTnNvT51cKEY
WQPJIrSPnNVeKtelttQKbfi3QBFGmh95DmK/D5fs4C8fF5Q=
-----END CERTIFICATE-----");

[!Note]

In order to get a root certificate from a server, if you install OpenSSL, you can run the following command: openssl s_client -connect the.server-to-get-the-cert.com:443 -showcerts This will show you all the root certs used and present them as PEM. You can then copy/paste the one you need.

It's possible to add HTTP headers that will be sent along with each request.

_httpClient.DefaultRequestHeaders.Add("x-ms-blob-type", "BlockBlob");

Perform a HTTP GET request

Here's a example of a HTTP request to read some content as a string:

HttpResponseMessage response = _httpClient.Get("https://httpbin.org/get");
response.EnsureSuccessStatusCode();
var responseBody = response.Content.ReadAsString();

The above call would return something similar to the following, which can be output in Visual Studio by calling Debug.WriteLine(responseBody):

{
  "args": {}, 
  "headers": {
    "Host": "httpbin.org", 
    "X-Amzn-Trace-Id": "Root=1-6214aad3-38e5f8357bdf90530300eb5f", 
    "X-Ms-Blob-Type": "BlockBlob"
  }, 
  "origin": "5.249.47.208", 
  "url": "https://httpbin.org/get"
}

Note the call to response.EnsureSuccessStatusCode();. This will throw an HttpRequestException in case the status code from the HTTP request is not a successful one.

Perform a HTTP POST request

Follows an example of a HTTP request performing POST request to send some json content to an endpoint.

var content = new StringContent("{\"someProperty\":\"someValue\"}", Encoding.UTF8, "application/json");
var result = _httpClient.Post("https://httpbin.org/anything", content);
result.EnsureSuccessStatusCode();

Worth noting that the json content above it's presented as a simple string to simplify the code. There is a json library available to help with serializing and deserializing from/to C# classes, even the most complex ones.

Again, note the call to response.EnsureSuccessStatusCode(); to make sure the HTTP request was successfully performed.

Download binary content to a file

Using HttpClient makes it easy to deal with binary content too. Here's an example of how to download a file from a webserver.

HttpResponseMessage response = _httpClient.Get("https://storage-on-the-cloud.net/file-with-binary-content");
response.EnsureSuccessStatusCode();

using FileStream fs = new FileStream($"I:\\i-am-a-binary-file.bin", FileMode.Create, FileAccess.Write);
response.Content.ReadAsStream().CopyTo(fs);

Disposing the response

[!Important] You MUST dispose the response sent but any of the request. The device memory is limited and the response content will never be disposed if not asked. Depending on the devices, you may have only 16 possible requests before you'll get an out of memory.

Here is few right patterns to make sure you'll dispose properly the response content:

using HttpResponseMessage response = client.Get(apiUrl);
// do whatever you want
// When the response object won't be used, it will be disposed

You can as well have an explicit using for a block:

using HttpResponseMessage response = client.Get(apiUrl)
{
  // do whatever you want
  // when exiting the block, the response will be disposed
}

Or explicitly dispose the response:

HttpResponseMessage response = client.Get(apiUrl);
// do whatever you want
// Dispose explicitly the content
response.Dispose();

In all cases, you MUST make ure you are disposing the response content.

Debugging through a reverse proxy

When code is deployed to a MCU it might be desirable to let the device connect to your development machine running IIS Express. This can be achieved with a proxy such as this one. Be aware that this leads to SocketExceptions with the current version of nanoFramework System.Net.Http when sending consecutive requests to your development machine. A simple retry mechanism in Debug mode will get around this.

Feedback and documentation

For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.

Join our Discord community here.

Credits

The list of contributors to this project can be found at CONTRIBUTORS.

License

The nanoFramework Class Libraries are licensed under the MIT license.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on nanoFramework.System.Net.Http.Server:

Package Downloads
nanoFramework.WebServer

This is a simple multithread WebServer supporting simple controller and event based calls. Perfect for .NET nanoFramework REST API based project. Supports both HTTPS and HTTP. If serving files from local storage is a requirement, please use instead the 'nanoFramework.WebServer.FileSystem' nuget.

CCSWE.nanoFramework.WebServer

A simple asynchronous web server implementation for nanoFramework.

nanoFramework.WebServer.FileSystem

This is a simple multithread WebServer supporting simple controller and event based calls. Perfect for .NET nanoFramework REST API based project. Support all type of Http Methods. Perfect for simple embedded web pages, with Support of file on a storage (USB, SD Card, in Memory). Supports both HTTPS and HTTP. Use this version if you want to serve local files and have support for System.IO.FileSystem on your device. Otherwise use 'nanoFramework.WebServer' nuget.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on nanoFramework.System.Net.Http.Server:

Repository Stars
nanoframework/Samples
🍬 Code samples from the nanoFramework team used in testing, proof of concepts and other explorational endeavours
Version Downloads Last updated
1.5.150 1,031 9/26/2024
1.5.145 325 7/30/2024
1.5.138 779 5/13/2024
1.5.136 277 5/10/2024
1.5.134 490 4/8/2024
1.5.132 217 4/8/2024
1.5.130 319 4/3/2024
1.5.128 214 4/3/2024
1.5.125 645 1/29/2024
1.5.118 435 1/24/2024
1.5.113 3,892 11/10/2023
1.5.110 155 11/9/2023
1.5.108 249 11/9/2023
1.5.106 347 11/3/2023
1.5.104 533 10/10/2023
1.5.102 309 10/4/2023
1.5.99 588 8/8/2023
1.5.97 360 7/27/2023
1.5.95 236 7/27/2023
1.5.84 1,100 2/17/2023
1.5.82 590 1/24/2023
1.5.78 1,038 12/28/2022
1.5.75 359 12/28/2022
1.5.73 355 12/28/2022
1.5.70 515 12/27/2022
1.5.68 388 12/22/2022
1.5.61 680 11/23/2022
1.5.54 841 10/26/2022
1.5.52 422 10/26/2022
1.5.50 481 10/26/2022
1.5.48 625 10/25/2022
1.5.45 441 10/25/2022
1.5.43 672 10/24/2022
1.5.41 477 10/24/2022
1.5.39 450 10/24/2022
1.5.37 462 10/23/2022
1.5.35 711 10/23/2022
1.5.30 753 10/10/2022
1.5.27 443 10/8/2022
1.5.24 1,004 9/22/2022
1.5.22 747 9/22/2022
1.5.20 1,364 9/15/2022
1.5.18 505 9/15/2022
1.5.13 1,301 8/5/2022
1.5.10 731 8/4/2022
1.5.8 478 8/4/2022
1.5.6 451 8/4/2022
1.5.4 723 8/3/2022
1.5.2 738 8/3/2022
1.4.0.25 726 8/3/2022
1.4.0.23 1,068 6/13/2022
1.4.0.21 792 6/8/2022
1.4.0.19 750 6/8/2022
1.4.0.14 860 5/26/2022
1.4.0.12 556 5/18/2022
1.4.0.10 566 5/3/2022
1.4.0 1,045 3/28/2022
1.4.0-preview.52 145 3/28/2022
1.4.0-preview.51 135 3/28/2022
1.4.0-preview.50 134 3/28/2022
1.4.0-preview.48 126 3/28/2022
1.4.0-preview.47 133 3/28/2022
1.4.0-preview.46 134 3/28/2022
1.4.0-preview.44 171 3/17/2022
1.4.0-preview.43 138 3/17/2022
1.4.0-preview.42 148 3/14/2022
1.4.0-preview.41 126 3/14/2022
1.4.0-preview.40 132 3/14/2022
1.4.0-preview.39 135 3/14/2022
1.4.0-preview.37 136 3/14/2022
1.4.0-preview.36 149 3/8/2022
1.4.0-preview.35 135 3/8/2022
1.4.0-preview.33 158 3/4/2022
1.4.0-preview.32 131 3/3/2022
1.4.0-preview.31 137 3/2/2022
1.4.0-preview.29 153 2/28/2022
1.4.0-preview.28 171 2/24/2022
1.4.0-preview.26 158 2/17/2022
1.4.0-preview.24 147 2/17/2022
1.4.0-preview.22 188 2/6/2022
1.4.0-preview.21 159 2/4/2022
1.4.0-preview.19 152 2/4/2022
1.4.0-preview.18 163 1/28/2022
1.4.0-preview.16 155 1/28/2022
1.4.0-preview.15 151 1/28/2022
1.4.0-preview.13 151 1/28/2022
1.4.0-preview.12 153 1/21/2022
1.4.0-preview.6 252 1/12/2022
1.4.0-preview.5 151 1/12/2022
1.4.0-preview.4 147 1/11/2022
1.4.0-preview.3 155 1/11/2022
1.3.7-preview.8 169 1/5/2022
1.3.7-preview.7 168 1/3/2022
1.3.7-preview.6 163 1/3/2022
1.3.7-preview.5 153 1/3/2022
1.3.7-preview.4 155 12/30/2021
1.3.7-preview.3 171 12/28/2021
1.3.6 596 12/3/2021
1.3.6-preview.23 170 12/3/2021
1.3.6-preview.21 157 12/3/2021
1.3.6-preview.20 159 12/2/2021
1.3.6-preview.19 168 12/2/2021
1.3.6-preview.18 159 12/2/2021
1.3.6-preview.17 159 12/2/2021
1.3.6-preview.16 163 12/2/2021
1.3.6-preview.14 163 12/2/2021
1.3.6-preview.12 165 12/2/2021
1.3.6-preview.10 175 12/1/2021
1.3.6-preview.7 166 12/1/2021
1.3.6-preview.1 317 11/11/2021
1.3.5-preview.8 243 10/22/2021
1.3.5-preview.5 206 10/18/2021
1.3.4 896 7/16/2021
1.3.4-preview.78 160 7/16/2021
1.3.4-preview.76 180 7/16/2021
1.3.4-preview.74 168 7/16/2021
1.3.4-preview.72 175 7/15/2021
1.3.4-preview.70 190 7/14/2021
1.3.4-preview.68 329 6/19/2021
1.3.4-preview.66 275 6/19/2021
1.3.4-preview.64 186 6/17/2021
1.3.4-preview.62 221 6/7/2021
1.3.4-preview.60 188 6/7/2021
1.3.4-preview.58 223 6/7/2021
1.3.4-preview.56 562 6/6/2021
1.3.4-preview.54 579 6/3/2021
1.3.4-preview.52 181 6/2/2021
1.3.4-preview.50 194 6/1/2021
1.3.4-preview.48 200 6/1/2021
1.3.4-preview.41 229 5/25/2021
1.3.4-preview.39 241 5/21/2021
1.3.4-preview.37 239 5/20/2021
1.3.4-preview.35 191 5/19/2021
1.3.4-preview.33 186 5/19/2021
1.3.4-preview.31 199 5/19/2021
1.3.4-preview.29 184 5/15/2021
1.3.4-preview.27 174 5/15/2021
1.3.4-preview.25 171 5/15/2021
1.3.4-preview.23 208 5/13/2021
1.3.4-preview.22 175 5/13/2021
1.3.4-preview.20 213 5/11/2021
1.3.4-preview.18 249 5/6/2021
1.3.4-preview.16 172 5/6/2021
1.3.4-preview.14 174 5/5/2021
1.3.4-preview.13 168 5/5/2021
1.3.4-preview.10 266 4/10/2021
1.3.4-preview.9 210 4/9/2021
1.3.4-preview.7 213 4/5/2021
1.3.3-preview.65 263 3/20/2021
1.3.3-preview.62 222 3/19/2021
1.3.3-preview.60 186 3/17/2021
1.3.3-preview.57 176 3/17/2021
1.3.3-preview.55 250 3/2/2021
1.3.3-preview.50 541 1/19/2021
1.3.3-preview.47 225 1/6/2021
1.3.3-preview.40 420 12/29/2020
1.3.3-preview.34 296 12/7/2020
1.3.3-preview.32 224 12/7/2020
1.3.3-preview.27 350 11/3/2020
1.3.3-preview.25 233 10/26/2020
1.3.3-preview.23 271 10/21/2020
1.3.3-preview.17 258 10/20/2020
1.3.3-preview.15 323 10/1/2020
1.3.3-preview.11 243 9/30/2020
1.3.3-preview.9 252 9/30/2020
1.3.3-preview.7 268 9/27/2020
1.3.3-preview.5 276 9/27/2020
1.3.3-preview.1 242 9/24/2020
1.3.2-preview.3 264 9/19/2020
1.3.2-preview.1 270 8/25/2020
1.3.1-preview.6 284 7/2/2020
1.3.1-preview.3 317 6/30/2020
1.3.0 598 6/16/2020
1.3.0-rc.3 298 6/16/2020
1.3.0-preview.11 311 6/16/2020
1.3.0-preview.9 245 6/16/2020
1.3.0-preview.7 240 6/16/2020
1.3.0-alpha.12 310 6/16/2020
1.3.0-alpha.10 238 6/16/2020
1.3.0-alpha.8 244 6/16/2020
1.2.2 495 6/12/2020
1.2.2-preview.41 247 6/12/2020
1.2.2-preview.39 266 6/12/2020
1.2.2-preview.34 257 6/3/2020
1.2.2-preview.33 272 6/3/2020
1.2.2-preview.31 355 5/31/2020
1.2.2-preview.30 257 5/31/2020
1.2.2-preview.29 293 5/31/2020
1.2.2-preview.28 277 5/29/2020
1.2.2-preview.27 269 5/8/2020
1.2.2-preview.26 272 5/8/2020
1.2.2-preview.25 257 4/27/2020
1.2.2-preview.24 256 4/24/2020
1.2.2-preview.23 242 4/21/2020
1.2.2-preview.22 288 4/16/2020
1.2.2-preview.19 281 4/16/2020
1.2.2-preview.18 256 4/14/2020
1.2.2-preview.17 253 4/14/2020
1.2.2-preview.16 250 4/14/2020
1.2.2-preview.15 291 3/25/2020
1.2.2-preview.14 289 3/15/2020
1.2.2-preview.13 262 3/10/2020
1.2.2-preview.12 252 3/10/2020
1.2.2-preview.10 260 3/10/2020
1.2.2-preview.8 253 3/10/2020
1.2.2-preview.7 259 3/10/2020
1.2.2-preview.6 281 3/9/2020
1.2.2-preview.5 284 3/9/2020
1.2.2-preview.4 276 3/6/2020
1.2.2-preview.3 302 1/13/2020
1.2.2-preview.2 284 11/14/2019
1.2.2-preview.1 277 11/12/2019
1.2.2-alpha.42 243 6/12/2020
1.2.2-alpha.40 275 6/12/2020
1.2.2-alpha.38 289 6/11/2020
1.2.2-alpha.35 247 6/3/2020
1.2.2-alpha.34 272 6/3/2020
1.2.2-alpha.32 362 5/31/2020
1.2.2-alpha.31 279 5/31/2020
1.2.2-alpha.30 280 5/30/2020
1.2.2-alpha.29 275 5/29/2020
1.2.2-alpha.28 272 5/8/2020
1.2.2-alpha.27 272 5/8/2020
1.2.2-alpha.26 248 4/27/2020
1.2.2-alpha.23 284 4/16/2020
1.2.2-alpha.21 284 4/16/2020
1.2.2-alpha.20 284 4/16/2020
1.2.2-alpha.19 256 4/14/2020
1.2.2-alpha.18 251 4/14/2020
1.2.2-alpha.17 257 4/14/2020
1.2.2-alpha.16 297 3/25/2020
1.2.2-alpha.15 293 3/15/2020
1.2.2-alpha.14 247 3/10/2020
1.2.2-alpha.13 253 3/10/2020
1.2.2-alpha.11 254 3/10/2020
1.2.2-alpha.10 257 3/10/2020
1.2.2-alpha.9 259 3/10/2020
1.2.2-alpha.8 281 3/9/2020
1.2.2-alpha.7 274 3/9/2020
1.2.2-alpha.6 301 3/9/2020
1.2.1-preview.3 277 11/8/2019
1.2.1-preview.1 288 11/5/2019
1.2.0-preview.3 267 10/18/2019
1.1.0 578 10/17/2019
1.1.0-preview.3 278 10/17/2019
1.0.8-preview.7 273 10/17/2019
1.0.8-preview.5 284 10/15/2019
1.0.8-preview.4 278 9/30/2019
1.0.8-preview.1 286 8/30/2019
1.0.7-preview.1 294 8/30/2019
1.0.6-preview.33 293 8/19/2019
1.0.6-preview.30 295 7/19/2019
1.0.6-preview.26 296 6/23/2019
1.0.6-preview.19 319 6/22/2019
1.0.6-preview.17 322 6/18/2019
1.0.5-preview-028 545 4/5/2019