In the previous post Wifi using nanoFramework on the M5Core2 we looked at connecting our M5Core2 using it’s wireless network capability, to our WiFi network. Next, let’s look at how we access a website, for example I’ll try to access the Cheer Lights API.
We’ll need to add the NuGet package nanoFramework.System.Net.Http.Client which exposes the HttpClient functionality for the nanoFramework.
Just like the full .NET framework/and core. We should create an HttpClient for the lifetime of the application. So we’d have something like this
public static class CheerLights { private static readonly HttpClient HttpClient = new HttpClient(); }
The Cheer Lights API is a simple call to https://api.thingspeak.com/channels/1417/field/2/last.txt which will return a #hex colour, for example #008000. So. we might write some code, such as this in a funtion within the CheerLights class
var requestUri = "https://api.thingspeak.com/channels/1417/field/2/last.txt"; var response = HttpClient.Get(requestUri); response.EnsureSuccessStatusCode(); var responseBody = response.Content.ReadAsString();
This would work for a non-HTTPS site, but for HTTPS requires that we get the CA certificate for the site we want to interact with (I haven’t yet found a way to use HTTPS without this).
To get the certificate, navigate to the page using a browser (I’m using Microsoft EDGE). Click on the padlock, click on connection is secure, then click on the show certificate button. Select Details then in the Certificate Hierarchy select the root CA certificate and export this (renaming as a txt file). This will be what we used for the HttpsAuthentCert (as we’ll see in a moment).
Now we want to include the certificate which we can do as a resource or just embedding the text into the code. So now we’d have something like this
try { HttpClient.HttpsAuthentCert = new X509Certificate( @"-----BEGIN CERTIFICATE----- MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3 d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG 9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97 nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt 43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4 gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg 06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4= -----END CERTIFICATE-----"); var requestUri = "https://api.thingspeak.com/channels/1417/field/2/last.txt"; var response = HttpClient.Get(requestUri); response.EnsureSuccessStatusCode(); var responseBody = response.Content.ReadAsString();