nanoFramework.System.Net.Http 1.4.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package nanoFramework.System.Net.Http --version 1.4.0                
NuGet\Install-Package nanoFramework.System.Net.Http -Version 1.4.0                
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" Version="1.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.System.Net.Http --version 1.4.0                
#r "nuget: nanoFramework.System.Net.Http, 1.4.0"                
#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 as a Cake Addin
#addin nuget:?package=nanoFramework.System.Net.Http&version=1.4.0

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

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 (preview) Build Status NuGet
System.Net.Http.Client Build Status NuGet
System.Net.Http.Client (preview) Build Status NuGet
System.Net.Http.Server Build Status NuGet
System.Net.Http.Server (preview) 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-----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WdqbAYcaT1epoXkJKtv3
L7IezMdeatiDh6GX70k1PncGQVhiv45YuApnP+yz3SFmH8lU+nLMPUxA2IGvd56D
eruix/U0F47ZEUD0/CwqTRV/p2JdLiXTAAsgGh1o+Re49L2L7ShZ3U0WixeDyLJl
xy16paq8U4Zt3VekyvggQQto8PT7dL5WXXp59fkdheMtlb71cZBDzI0fmgAKhynp
VSJYACPq4xJDKVtHCN2MQWplBqjlIapBtJUhlbl90TSrE9atvNziPTnNvT51cKEY
WQPJIrSPnNVeKtelttQKbfi3QBFGmh95DmK/D5fs4C8fF5Q=
-----END CERTIFICATE-----");

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

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 (14)

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

Package Downloads
nanoFramework.Azure.Devices.Client

This package includes the .NET nanoFramework.Azure.Devices.Client assembly for .NET nanoFramework C# projects. This is an SDK for Azure IoT Hub using MQTT broker.

nanoFramework.System.Net.WebSockets

This package includes the System.Net.WebSockets assembly for nanoFramework C# projects. There are also two other packages with just the client API and just the server API. These are meant to be used when there is the need to use smaller assemblies.

nanoFramework.System.Net.WebSockets.Server

This package includes the System.Net.WebSockets assembly (server API only) for .NET nanoFramework C# projects. There are also two other packages, one with the client API only and another with the complete API.

MakoIoT.Device.Services.ConfigurationManager

Configuration mode manager for MAKO-IoT

MakoIoT.Device.Services.Server

Simple WebServer for .NET nanoFramework C# projects. Based on nanoFramework.WebServer by Laurent Ellerbach and contributors

GitHub repositories (3)

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

Repository Stars
dotnet/samples
Sample code referenced by the .NET documentation
nanoframework/Samples
🍬 Code samples from the nanoFramework team used in testing, proof of concepts and other explorational endeavours
nanoframework/nanoFramework.IoT.Device
📦 This repo includes .NET nanoFramework implementations for various sensors, chips, displays, hats and drivers
Version Downloads Last updated
1.5.150 2,530 9/26/2024
1.5.145 3,449 7/30/2024
1.5.138 4,521 5/13/2024
1.5.136 644 5/10/2024
1.5.134 1,911 4/8/2024
1.5.132 528 4/8/2024
1.5.130 862 4/3/2024
1.5.128 243 4/3/2024
1.5.125 3,476 1/29/2024
1.5.118 1,735 1/24/2024
1.5.113 8,401 11/10/2023
1.5.110 142 11/9/2023
1.5.108 954 11/9/2023
1.5.106 1,644 11/3/2023
1.5.104 2,164 10/10/2023
1.5.102 2,519 10/4/2023
1.5.99 4,448 8/8/2023
1.5.97 881 7/27/2023
1.5.95 232 7/27/2023
1.5.84 5,904 2/17/2023
1.5.82 1,924 1/24/2023
1.5.78 2,708 12/28/2022
1.5.75 526 12/28/2022
1.5.73 514 12/28/2022
1.5.70 1,728 12/27/2022
1.5.68 447 12/22/2022
1.5.61 2,704 11/23/2022
1.5.54 3,518 10/26/2022
1.5.52 2,348 10/26/2022
1.5.50 1,914 10/26/2022
1.5.48 2,151 10/25/2022
1.5.45 957 10/25/2022
1.5.43 2,381 10/24/2022
1.5.41 970 10/24/2022
1.5.39 440 10/24/2022
1.5.37 2,232 10/23/2022
1.5.35 436 10/23/2022
1.5.30 3,368 10/10/2022
1.5.27 3,642 10/8/2022
1.5.24 4,033 9/22/2022
1.5.22 4,179 9/22/2022
1.5.20 3,893 9/15/2022
1.5.18 1,030 9/15/2022
1.5.13 3,201 8/5/2022
1.5.10 3,848 8/4/2022
1.5.8 759 8/4/2022
1.5.6 977 8/4/2022
1.5.4 2,313 8/3/2022
1.5.2 1,029 8/3/2022
1.4.0.25 739 8/3/2022
1.4.0.23 3,577 6/13/2022
1.4.0.21 2,624 6/8/2022
1.4.0.19 2,246 6/8/2022
1.4.0.14 2,430 5/26/2022
1.4.0.12 4,479 5/18/2022
1.4.0.10 4,244 5/3/2022
1.4.0 4,381 3/28/2022
1.4.0-preview.52 145 3/28/2022
1.4.0-preview.51 143 3/28/2022
1.4.0-preview.50 144 3/28/2022
1.4.0-preview.48 144 3/28/2022
1.4.0-preview.47 150 3/28/2022
1.4.0-preview.46 148 3/28/2022
1.4.0-preview.44 199 3/17/2022
1.4.0-preview.43 143 3/17/2022
1.4.0-preview.42 183 3/14/2022
1.4.0-preview.41 146 3/14/2022
1.4.0-preview.40 136 3/14/2022
1.4.0-preview.39 149 3/14/2022
1.4.0-preview.37 144 3/14/2022
1.4.0-preview.36 237 3/8/2022
1.4.0-preview.35 161 3/8/2022
1.4.0-preview.33 159 3/4/2022
1.4.0-preview.32 138 3/3/2022
1.4.0-preview.31 145 3/2/2022
1.4.0-preview.29 195 2/28/2022
1.4.0-preview.28 232 2/24/2022
1.4.0-preview.26 191 2/17/2022
1.4.0-preview.24 161 2/17/2022
1.4.0-preview.22 239 2/6/2022
1.4.0-preview.21 179 2/4/2022
1.4.0-preview.19 155 2/4/2022
1.4.0-preview.18 219 1/28/2022
1.4.0-preview.16 152 1/28/2022
1.4.0-preview.15 154 1/28/2022
1.4.0-preview.13 153 1/28/2022
1.4.0-preview.12 177 1/21/2022
1.4.0-preview.6 267 1/12/2022
1.4.0-preview.5 148 1/12/2022
1.4.0-preview.4 163 1/11/2022
1.4.0-preview.3 154 1/11/2022
1.3.7-preview.8 185 1/5/2022
1.3.7-preview.7 183 1/3/2022
1.3.7-preview.6 159 1/3/2022
1.3.7-preview.5 153 1/3/2022
1.3.7-preview.4 173 12/30/2021
1.3.7-preview.3 168 12/28/2021
1.3.6 759 12/3/2021
1.3.6-preview.23 175 12/3/2021
1.3.6-preview.21 157 12/3/2021
1.3.6-preview.20 177 12/2/2021
1.3.6-preview.19 155 12/2/2021
1.3.6-preview.18 167 12/2/2021
1.3.6-preview.17 173 12/2/2021
1.3.6-preview.16 161 12/2/2021
1.3.6-preview.14 163 12/2/2021
1.3.6-preview.12 157 12/2/2021
1.3.6-preview.10 177 12/1/2021
1.3.6-preview.7 165 12/1/2021
1.3.6-preview.1 337 11/11/2021
1.3.5-preview.8 292 10/22/2021
1.3.5-preview.5 208 10/18/2021
1.3.4 1,549 7/16/2021
1.3.4-preview.78 168 7/16/2021
1.3.4-preview.76 169 7/16/2021
1.3.4-preview.74 165 7/16/2021
1.3.4-preview.72 179 7/15/2021
1.3.4-preview.70 184 7/14/2021
1.3.4-preview.68 700 6/19/2021
1.3.4-preview.66 269 6/19/2021
1.3.4-preview.64 188 6/17/2021
1.3.4-preview.62 214 6/7/2021
1.3.4-preview.60 176 6/7/2021
1.3.4-preview.58 209 6/7/2021
1.3.4-preview.56 215 6/6/2021
1.3.4-preview.54 233 6/3/2021
1.3.4-preview.52 182 6/2/2021
1.3.4-preview.50 190 6/1/2021
1.3.4-preview.48 192 6/1/2021
1.3.4-preview.41 212 5/25/2021
1.3.4-preview.39 238 5/21/2021
1.3.4-preview.37 240 5/20/2021
1.3.4-preview.35 185 5/19/2021
1.3.4-preview.33 185 5/19/2021
1.3.4-preview.31 195 5/19/2021
1.3.4-preview.29 177 5/15/2021
1.3.4-preview.27 177 5/15/2021
1.3.4-preview.25 173 5/15/2021
1.3.4-preview.23 198 5/13/2021
1.3.4-preview.22 191 5/13/2021
1.3.4-preview.20 199 5/11/2021
1.3.4-preview.18 244 5/6/2021
1.3.4-preview.16 168 5/6/2021
1.3.4-preview.14 161 5/5/2021
1.3.4-preview.13 163 5/5/2021
1.3.4-preview.10 223 4/10/2021
1.3.4-preview.9 222 4/9/2021
1.3.4-preview.7 201 4/5/2021
1.3.3-preview.65 255 3/20/2021
1.3.3-preview.62 210 3/19/2021
1.3.3-preview.60 173 3/17/2021
1.3.3-preview.57 180 3/17/2021
1.3.3-preview.55 212 3/2/2021
1.3.3-preview.50 412 1/19/2021
1.3.3-preview.47 215 1/6/2021
1.3.3-preview.40 197 12/29/2020
1.3.3-preview.34 267 12/7/2020
1.3.3-preview.32 226 12/7/2020
1.3.3-preview.27 275 11/3/2020
1.3.3-preview.25 247 10/26/2020
1.3.3-preview.23 273 10/21/2020
1.3.3-preview.17 267 10/20/2020
1.3.3-preview.15 324 10/1/2020
1.3.3-preview.11 244 9/30/2020
1.3.3-preview.9 247 9/30/2020
1.3.3-preview.7 273 9/27/2020
1.3.3-preview.5 283 9/27/2020
1.3.3-preview.1 244 9/24/2020
1.3.2-preview.3 271 9/19/2020
1.3.2-preview.1 342 8/25/2020
1.3.1-preview.6 288 7/2/2020
1.3.1-preview.3 323 6/30/2020
1.3.0 787 6/16/2020
1.3.0-rc.3 301 6/16/2020
1.3.0-preview.11 302 6/16/2020
1.3.0-preview.9 250 6/16/2020
1.3.0-preview.7 245 6/16/2020
1.3.0-alpha.12 301 6/16/2020
1.3.0-alpha.10 236 6/16/2020
1.3.0-alpha.8 253 6/16/2020
1.2.2 496 6/12/2020
1.2.2-preview.41 249 6/12/2020
1.2.2-preview.39 263 6/12/2020
1.2.2-preview.34 269 6/3/2020
1.2.2-preview.33 279 6/3/2020
1.2.2-preview.31 377 5/31/2020
1.2.2-preview.30 267 5/31/2020
1.2.2-preview.29 290 5/31/2020
1.2.2-preview.28 273 5/29/2020
1.2.2-preview.27 306 5/8/2020
1.2.2-preview.26 274 5/8/2020
1.2.2-preview.25 283 4/27/2020
1.2.2-preview.24 266 4/24/2020
1.2.2-preview.23 246 4/21/2020
1.2.2-preview.22 299 4/16/2020
1.2.2-preview.19 297 4/16/2020
1.2.2-preview.18 258 4/14/2020
1.2.2-preview.17 265 4/14/2020
1.2.2-preview.16 255 4/14/2020
1.2.2-preview.15 308 3/25/2020
1.2.2-preview.14 309 3/15/2020
1.2.2-preview.13 261 3/10/2020
1.2.2-preview.12 267 3/10/2020
1.2.2-preview.10 249 3/10/2020
1.2.2-preview.8 260 3/10/2020
1.2.2-preview.7 258 3/10/2020
1.2.2-preview.6 284 3/9/2020
1.2.2-preview.5 285 3/9/2020
1.2.2-preview.4 276 3/6/2020
1.2.2-preview.3 340 1/13/2020
1.2.2-preview.2 341 11/14/2019
1.2.2-preview.1 285 11/12/2019
1.2.2-alpha.42 247 6/12/2020
1.2.2-alpha.40 262 6/12/2020
1.2.2-alpha.38 291 6/11/2020
1.2.2-alpha.35 263 6/3/2020
1.2.2-alpha.34 273 6/3/2020
1.2.2-alpha.32 356 5/31/2020
1.2.2-alpha.31 287 5/31/2020
1.2.2-alpha.30 288 5/30/2020
1.2.2-alpha.29 266 5/29/2020
1.2.2-alpha.28 274 5/8/2020
1.2.2-alpha.27 269 5/8/2020
1.2.2-alpha.26 253 4/27/2020
1.2.2-alpha.23 291 4/16/2020
1.2.2-alpha.21 292 4/16/2020
1.2.2-alpha.20 284 4/16/2020
1.2.2-alpha.19 245 4/14/2020
1.2.2-alpha.18 254 4/14/2020
1.2.2-alpha.17 253 4/14/2020
1.2.2-alpha.16 303 3/25/2020
1.2.2-alpha.15 299 3/15/2020
1.2.2-alpha.14 254 3/10/2020
1.2.2-alpha.13 250 3/10/2020
1.2.2-alpha.11 253 3/10/2020
1.2.2-alpha.10 257 3/10/2020
1.2.2-alpha.9 265 3/10/2020
1.2.2-alpha.8 287 3/9/2020
1.2.2-alpha.7 287 3/9/2020
1.2.2-alpha.6 303 3/9/2020
1.2.1-preview.3 276 11/8/2019
1.2.1-preview.1 288 11/5/2019
1.2.0-preview.3 302 10/18/2019
1.1.0 659 10/17/2019
1.1.0-preview.3 280 10/17/2019
1.0.8-preview.7 272 10/17/2019
1.0.8-preview.5 290 10/15/2019
1.0.8-preview.4 290 9/30/2019
1.0.8-preview.1 303 8/30/2019
1.0.7-preview.1 295 8/30/2019
1.0.6-preview.33 296 8/19/2019
1.0.6-preview.30 314 7/19/2019
1.0.6-preview.26 420 6/23/2019
1.0.6-preview.19 328 6/20/2019
1.0.6-preview.17 326 6/14/2019
1.0.6-preview.14 351 6/12/2019
1.0.6-preview.10 325 6/12/2019
1.0.6-preview.6 339 6/5/2019
1.0.6-preview.4 344 6/5/2019
1.0.6-preview.2 330 5/31/2019
1.0.5-preview-038 579 4/24/2019
1.0.5-preview-036 585 4/23/2019
1.0.5-preview-031 500 4/7/2019
1.0.5-preview-028 524 4/5/2019
1.0.5-preview-026 495 4/5/2019
1.0.5-preview-024 490 4/3/2019
1.0.5-preview-022 545 3/27/2019
1.0.5-preview-020 489 3/26/2019
1.0.5-preview-017 504 3/26/2019
1.0.5-preview-009 512 3/25/2019
1.0.5-preview-007 493 3/15/2019
1.0.5-preview-004 539 3/11/2019
1.0.5-preview-002 499 3/11/2019
1.0.5-preview-001 535 3/11/2019
1.0.4 1,041 1/22/2019
1.0.4-preview-009 525 3/10/2019
1.0.4-preview-003 651 1/3/2019
1.0.3 784 12/17/2018
1.0.2 772 11/15/2018
1.0.2-preview-009 649 11/20/2018
1.0.2-preview-005 628 11/8/2018