nanoFramework.System.Net.Http.Client 1.4.0.25

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.Client --version 1.4.0.25                
NuGet\Install-Package nanoFramework.System.Net.Http.Client -Version 1.4.0.25                
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.Client" Version="1.4.0.25" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.System.Net.Http.Client --version 1.4.0.25                
#r "nuget: nanoFramework.System.Net.Http.Client, 1.4.0.25"                
#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.Client as a Cake Addin
#addin nuget:?package=nanoFramework.System.Net.Http.Client&version=1.4.0.25

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

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-----
MIIEDzCCAvegAwIBAgIB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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

This package is not used by any NuGet packages.

GitHub repositories (2)

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

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
Version Downloads Last updated
1.5.150 332 9/26/2024
1.5.145 192 7/30/2024
1.5.138 292 5/13/2024
1.5.136 147 5/10/2024
1.5.134 209 4/8/2024
1.5.132 105 4/8/2024
1.5.130 156 4/3/2024
1.5.128 106 4/3/2024
1.5.125 303 1/29/2024
1.5.118 132 1/24/2024
1.5.113 406 11/10/2023
1.5.110 134 11/9/2023
1.5.108 151 11/9/2023
1.5.106 155 11/3/2023
1.5.104 249 10/10/2023
1.5.102 192 10/4/2023
1.5.99 332 8/8/2023
1.5.97 266 7/27/2023
1.5.95 150 7/27/2023
1.5.84 609 2/17/2023
1.5.82 383 1/24/2023
1.5.78 359 12/28/2022
1.5.75 317 12/28/2022
1.5.73 322 12/28/2022
1.5.70 326 12/27/2022
1.5.68 334 12/22/2022
1.5.61 531 11/23/2022
1.5.54 499 10/26/2022
1.5.52 369 10/26/2022
1.5.50 416 10/26/2022
1.5.48 404 10/25/2022
1.5.45 400 10/25/2022
1.5.43 427 10/24/2022
1.5.41 434 10/24/2022
1.5.39 408 10/24/2022
1.5.37 418 10/23/2022
1.5.35 423 10/23/2022
1.5.30 448 10/10/2022
1.5.27 391 10/8/2022
1.5.24 479 9/22/2022
1.5.22 432 9/22/2022
1.5.20 531 9/15/2022
1.5.18 448 9/15/2022
1.5.13 642 8/5/2022
1.5.10 413 8/4/2022
1.5.8 413 8/4/2022
1.5.6 438 8/4/2022
1.5.4 434 8/3/2022
1.5.2 441 8/3/2022
1.4.0.25 452 8/3/2022
1.4.0.23 609 6/13/2022
1.4.0.21 451 6/8/2022
1.4.0.19 451 6/8/2022
1.4.0.14 504 5/26/2022
1.4.0.12 479 5/18/2022
1.4.0.10 502 5/3/2022
1.4.0 593 3/28/2022
1.4.0-preview.52 132 3/28/2022
1.4.0-preview.51 123 3/28/2022
1.4.0-preview.50 121 3/28/2022
1.4.0-preview.48 122 3/28/2022
1.4.0-preview.47 122 3/28/2022
1.4.0-preview.46 123 3/28/2022
1.4.0-preview.44 141 3/17/2022
1.4.0-preview.43 123 3/17/2022
1.4.0-preview.42 130 3/14/2022
1.4.0-preview.41 127 3/14/2022
1.4.0-preview.40 118 3/14/2022
1.4.0-preview.39 122 3/14/2022
1.4.0-preview.37 127 3/14/2022
1.4.0-preview.36 139 3/8/2022
1.4.0-preview.35 130 3/8/2022
1.4.0-preview.33 138 3/4/2022
1.4.0-preview.32 127 3/3/2022
1.4.0-preview.31 129 3/2/2022
1.4.0-preview.29 167 2/28/2022
1.4.0-preview.28 195 2/24/2022
1.4.0-preview.26 148 2/17/2022
1.4.0-preview.24 129 2/17/2022
1.4.0-preview.22 157 2/6/2022
1.4.0-preview.21 148 2/4/2022
1.4.0-preview.19 147 2/4/2022
1.4.0-preview.18 146 1/28/2022
1.4.0-preview.16 142 1/28/2022
1.4.0-preview.15 146 1/28/2022
1.4.0-preview.13 147 1/28/2022
1.4.0-preview.12 143 1/21/2022
1.4.0-preview.6 159 1/12/2022
1.4.0-preview.5 143 1/12/2022
1.4.0-preview.4 140 1/11/2022
1.4.0-preview.3 143 1/11/2022
1.3.7-preview.8 142 1/5/2022
1.3.7-preview.7 155 1/3/2022
1.3.7-preview.6 143 1/3/2022
1.3.7-preview.5 145 1/3/2022
1.3.7-preview.4 143 12/30/2021
1.3.7-preview.3 145 12/28/2021
1.3.6 559 12/3/2021
1.3.6-preview.23 143 12/3/2021
1.3.6-preview.21 150 12/3/2021
1.3.6-preview.20 154 12/2/2021
1.3.6-preview.19 163 12/2/2021
1.3.6-preview.18 148 12/2/2021
1.3.6-preview.17 157 12/2/2021
1.3.6-preview.16 155 12/2/2021
1.3.6-preview.14 154 12/2/2021
1.3.6-preview.12 150 12/2/2021
1.3.6-preview.10 155 12/1/2021
1.3.6-preview.7 157 12/1/2021
1.3.6-preview.1 226 11/11/2021
1.3.5-preview.8 164 10/22/2021
1.3.5-preview.5 178 10/18/2021
1.3.4 1,005 7/16/2021
1.3.4-preview.78 163 7/16/2021
1.3.4-preview.76 164 7/16/2021
1.3.4-preview.74 169 7/16/2021
1.3.4-preview.72 182 7/15/2021
1.3.4-preview.70 179 7/14/2021
1.3.4-preview.68 615 6/19/2021
1.3.4-preview.66 268 6/19/2021
1.3.4-preview.64 169 6/17/2021
1.3.4-preview.62 180 6/7/2021
1.3.4-preview.60 174 6/7/2021
1.3.4-preview.58 201 6/7/2021
1.3.4-preview.56 212 6/6/2021
1.3.4-preview.54 213 6/3/2021
1.3.4-preview.52 170 6/2/2021
1.3.4-preview.50 181 6/1/2021
1.3.4-preview.48 192 6/1/2021
1.3.4-preview.41 202 5/25/2021
1.3.4-preview.39 228 5/21/2021
1.3.4-preview.37 230 5/20/2021
1.3.4-preview.35 173 5/19/2021
1.3.4-preview.33 174 5/19/2021
1.3.4-preview.31 196 5/19/2021
1.3.4-preview.29 174 5/15/2021
1.3.4-preview.27 173 5/15/2021
1.3.4-preview.25 175 5/15/2021
1.3.4-preview.23 180 5/13/2021
1.3.4-preview.22 174 5/13/2021
1.3.4-preview.20 193 5/11/2021
1.3.4-preview.18 237 5/6/2021
1.3.4-preview.16 177 5/6/2021
1.3.4-preview.14 163 5/5/2021
1.3.4-preview.13 161 5/5/2021
1.3.4-preview.10 194 4/10/2021
1.3.4-preview.9 206 4/9/2021
1.3.4-preview.7 181 4/5/2021
1.3.3-preview.65 233 3/20/2021
1.3.3-preview.62 209 3/19/2021
1.3.3-preview.60 171 3/17/2021
1.3.3-preview.57 172 3/17/2021
1.3.3-preview.55 187 3/2/2021
1.3.3-preview.50 387 1/19/2021
1.3.3-preview.47 192 1/6/2021
1.3.3-preview.40 186 12/29/2020
1.3.3-preview.34 257 12/7/2020
1.3.3-preview.32 220 12/7/2020
1.3.3-preview.27 244 11/3/2020
1.3.3-preview.25 249 10/26/2020
1.3.3-preview.23 264 10/21/2020
1.3.3-preview.17 249 10/20/2020
1.3.3-preview.15 318 10/1/2020
1.3.3-preview.11 244 9/30/2020
1.3.3-preview.9 246 9/30/2020
1.3.3-preview.7 265 9/27/2020
1.3.3-preview.5 268 9/27/2020
1.3.3-preview.1 239 9/24/2020
1.3.2-preview.3 263 9/19/2020
1.3.2-preview.1 325 8/25/2020
1.3.1-preview.6 282 7/2/2020
1.3.1-preview.3 320 6/30/2020
1.3.0 555 6/16/2020
1.3.0-rc.3 298 6/16/2020
1.3.0-preview.11 307 6/16/2020
1.3.0-preview.9 234 6/16/2020
1.3.0-preview.7 239 6/16/2020
1.3.0-alpha.12 300 6/16/2020
1.3.0-alpha.10 233 6/16/2020
1.3.0-alpha.8 239 6/16/2020
1.2.2 458 6/12/2020
1.2.2-preview.41 242 6/12/2020
1.2.2-preview.39 256 6/12/2020
1.2.2-preview.34 258 6/3/2020
1.2.2-preview.33 269 6/3/2020
1.2.2-preview.31 364 5/31/2020
1.2.2-preview.30 250 5/31/2020
1.2.2-preview.29 281 5/31/2020
1.2.2-preview.28 274 5/29/2020
1.2.2-preview.27 277 5/8/2020
1.2.2-preview.26 273 5/8/2020
1.2.2-preview.25 255 4/27/2020
1.2.2-preview.24 246 4/24/2020
1.2.2-preview.23 242 4/21/2020
1.2.2-preview.22 294 4/16/2020
1.2.2-preview.19 284 4/16/2020
1.2.2-preview.18 261 4/14/2020
1.2.2-preview.17 253 4/14/2020
1.2.2-preview.16 261 4/14/2020
1.2.2-preview.15 287 3/25/2020
1.2.2-preview.14 297 3/15/2020
1.2.2-preview.13 259 3/10/2020
1.2.2-preview.12 251 3/10/2020
1.2.2-preview.10 249 3/10/2020
1.2.2-preview.8 255 3/10/2020
1.2.2-preview.7 255 3/10/2020
1.2.2-preview.6 277 3/9/2020
1.2.2-preview.5 278 3/9/2020
1.2.2-preview.4 256 3/6/2020
1.2.2-preview.3 307 1/13/2020
1.2.2-preview.2 317 11/14/2019
1.2.2-preview.1 269 11/12/2019
1.2.2-alpha.42 241 6/12/2020
1.2.2-alpha.40 264 6/12/2020
1.2.2-alpha.38 293 6/11/2020
1.2.2-alpha.35 257 6/3/2020
1.2.2-alpha.34 266 6/3/2020
1.2.2-alpha.32 359 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 266 5/29/2020
1.2.2-alpha.28 273 5/8/2020
1.2.2-alpha.27 266 5/8/2020
1.2.2-alpha.26 250 4/27/2020
1.2.2-alpha.23 284 4/16/2020
1.2.2-alpha.21 274 4/16/2020
1.2.2-alpha.20 273 4/16/2020
1.2.2-alpha.19 249 4/14/2020
1.2.2-alpha.18 255 4/14/2020
1.2.2-alpha.17 247 4/14/2020
1.2.2-alpha.16 292 3/25/2020
1.2.2-alpha.15 297 3/15/2020
1.2.2-alpha.14 250 3/10/2020
1.2.2-alpha.13 242 3/10/2020
1.2.2-alpha.11 250 3/10/2020
1.2.2-alpha.10 259 3/10/2020
1.2.2-alpha.9 257 3/10/2020
1.2.2-alpha.8 281 3/9/2020
1.2.2-alpha.7 282 3/9/2020
1.2.2-alpha.6 291 3/9/2020
1.2.1-preview.3 266 11/8/2019
1.2.1-preview.1 274 11/5/2019
1.2.0-preview.3 268 10/18/2019
1.1.0 591 10/17/2019
1.1.0-preview.3 274 10/17/2019
1.0.8-preview.7 264 10/17/2019
1.0.8-preview.5 292 10/15/2019
1.0.8-preview.4 279 9/30/2019
1.0.8-preview.1 291 8/30/2019
1.0.7-preview.1 294 8/30/2019
1.0.6-preview.33 297 8/19/2019
1.0.6-preview.30 293 7/19/2019
1.0.6-preview.26 304 6/23/2019
1.0.6-preview.19 321 6/22/2019
1.0.6-preview.17 319 6/18/2019
1.0.5-preview-028 489 4/5/2019
1.0.5-preview-026 497 4/5/2019