Component | Build Status | NuGet Package |
---|---|---|
System.Net.Http | ||
System.Net.Http.Client | ||
System.Net.Http.Server |
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
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-----
MIIEDzCCAvegAwIBAgIBADANBgkqhkiG9w0BAQUFADBoMQswCQYDVQQGEwJVUzEl
MCMGA1UEChMcU3RhcmZpZWxkIFRlY2hub2xvZ2llcywgSW5jLjEyMDAGA1UECxMp
U3RhcmZpZWxkIENsYXNzIDIgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDQw
NjI5MTczOTE2WhcNMzQwNjI5MTczOTE2WjBoMQswCQYDVQQGEwJVUzElMCMGA1UE
ChMcU3RhcmZpZWxkIFRlY2hub2xvZ2llcywgSW5jLjEyMDAGA1UECxMpU3RhcmZp
ZWxkIENsYXNzIDIgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwggEgMA0GCSqGSIb3
DQEBAQUAA4IBDQAwggEIAoIBAQC3Msj+6XGmBIWtDBFk385N78gDGIc/oav7PKaf
8MOh2tTYbitTkPskpD6E8J7oX+zlJ0T1KKY/e97gKvDIr1MvnsoFAZMej2YcOadN
+lq2cwQlZut3f+dZxkqZJRRU6ybH838Z1TBwj6+wRir/resp7defqgSHo9T5iaU0
X9tDkYI22WY8sbi5gv2cOj4QyDvvBmVmepsZGD3/cVE8MC5fvj13c7JdBmzDI1aa
K4UmkhynArPkPw2vCHmCuDY96pzTNbO8acr1zJ3o/WSNF4Azbl5KXZnJHoe0nRrA
1W4TNSNe35tfPe/W93bC6j67eA0cQmdrBNj41tpvi/JEoAGrAgEDo4HFMIHCMB0G
A1UdDgQWBBS/X7fRzt0fhvRbVazc1xDCDqmI5zCBkgYDVR0jBIGKMIGHgBS/X7fR
zt0fhvRbVazc1xDCDqmI56FspGowaDELMAkGA1UEBhMCVVMxJTAjBgNVBAoTHFN0
YXJmaWVsZCBUZWNobm9sb2dpZXMsIEluYy4xMjAwBgNVBAsTKVN0YXJmaWVsZCBD
bGFzcyAyIENlcnRpZmljYXRpb24gQXV0aG9yaXR5ggEAMAwGA1UdEwQFMAMBAf8w
DQYJKoZIhvcNAQEFBQADggEBAAWdP4id0ckaVaGsafPzWdqbAYcaT1epoXkJKtv3
L7IezMdeatiDh6GX70k1PncGQVhiv45YuApnP+yz3SFmH8lU+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");
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.
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.
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);
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.
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.
For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.
Join our Discord community here.
The list of contributors to this project can be found at CONTRIBUTORS.
The nanoFramework Class Libraries are licensed under the MIT license.
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.
This project is supported by the .NET Foundation.