{"id":9704,"date":"2022-11-06T20:34:17","date_gmt":"2022-11-06T20:34:17","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9704"},"modified":"2022-11-06T20:34:17","modified_gmt":"2022-11-06T20:34:17","slug":"nanoframework-accessing-a-webservice-using-the-m5core2","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/nanoframework-accessing-a-webservice-using-the-m5core2\/","title":{"rendered":"nanoFramework accessing a webservice using the M5Core2"},"content":{"rendered":"<p>In the previous post <a href=\"http:\/\/putridparrot.com\/blog\/wifi-using-nanoframework-on-the-m5core2\/\" rel=\"noopener\" target=\"_blank\">Wifi using nanoFramework on the M5Core2<\/a> we looked at connecting our M5Core2 using it&#8217;s wireless network capability, to our WiFi network. Next, let&#8217;s look at how we access a website, for example I&#8217;ll try to access the <a href=\"https:\/\/cheerlights.com\/learn\/\" rel=\"noopener\" target=\"_blank\">Cheer Lights<\/a> API.<\/p>\n<p>We&#8217;ll need to add the NuGet package <em>nanoFramework.System.Net.Http.Client<\/em> which exposes the HttpClient functionality for the nanoFramework.<\/p>\n<p>Just like the full .NET framework\/and core. We should create an <em>HttpClient<\/em> for the lifetime of the application. So we&#8217;d have something like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static class CheerLights\r\n{\r\n   private static readonly HttpClient HttpClient = new HttpClient();\r\n}\r\n<\/pre>\n<p>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<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar requestUri = &quot;https:\/\/api.thingspeak.com\/channels\/1417\/field\/2\/last.txt&quot;;\r\n\r\nvar response = HttpClient.Get(requestUri);\r\nresponse.EnsureSuccessStatusCode();\r\nvar responseBody = response.Content.ReadAsString();\r\n<\/pre>\n<p>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&#8217;t yet found a way to use HTTPS without this).<\/p>\n<p>To get the certificate, navigate to the page using a browser (I&#8217;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&#8217;ll see in a moment).<\/p>\n<p>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&#8217;d have something like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ntry\r\n{\r\n   HttpClient.HttpsAuthentCert = new X509Certificate(\r\n                        @&quot;-----BEGIN CERTIFICATE-----\r\nMIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\r\nMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\r\nd3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\r\nQTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\r\nMRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\r\nb20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\r\n9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\r\nCSDMAZOnTjC3U\/dDxGkAV53ijSLdhwZAAIEJzs4bg7\/fzTtxRuLWZscFs3YnFo97\r\nnh6Vfe63SKMI2tavegw5BmV\/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\r\n43C\/dxC\/\/AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\r\nT19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr\/y7vrTC0LUq7dBMtoM1O\/4\r\ngdW7jVg\/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\r\nBgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH\/BAUwAwEB\/zAdBgNVHQ4EFgQUA95QNVbR\r\nTLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\r\nDQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY\/Esr\r\nhMAtudXH\/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp\/2PV5Adg\r\n06O\/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\r\nPnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\r\nYSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\r\nCAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\r\n-----END CERTIFICATE-----&quot;);\r\n\r\n   var requestUri = &quot;https:\/\/api.thingspeak.com\/channels\/1417\/field\/2\/last.txt&quot;;\r\n   var response = HttpClient.Get(requestUri);\r\n   response.EnsureSuccessStatusCode();\r\n   var responseBody = response.Content.ReadAsString();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post Wifi using nanoFramework on the M5Core2 we looked at connecting our M5Core2 using it&#8217;s wireless network capability, to our WiFi network. Next, let&#8217;s look at how we access a website, for example I&#8217;ll try to access the Cheer Lights API. We&#8217;ll need to add the NuGet package nanoFramework.System.Net.Http.Client which exposes the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[89,345,341],"tags":[],"class_list":["post-9704","post","type-post","status-publish","format-standard","hentry","category-electronics","category-m5core2","category-nanoframework"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9704"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9704\/revisions"}],"predecessor-version":[{"id":9707,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9704\/revisions\/9707"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}