I have issue while trying to update a thingspeak channel using https
I works fine with http but not with https.
I extract the certificate from edge and store it as a resource ( api.thingspeak.com
I tried with a winform app and get both to work without any issue.
with tinyClr, I got one WebnException (Invalid operation)
Here is the code used to test
Any idea ?
try
{
// HTTP
var url = “http://api.thingspeak.com/update?api_key=MyKey&field2=12.6”;
if (HttpWebRequest.Create(url) is HttpWebRequest request)
{
request.Method = "GET";
using var response = request.GetResponse() as HttpWebResponse;
if (response != null)
{
Debug.WriteLine(response is { StatusCode: HttpStatusCode.OK }
? "Data successfully sent to ThingSpeak (http)!"
: $"Failed to send data: {response.StatusCode}");
}
}
// HTTPS
url = "https://api.thingspeak.com/update?api_key=MyKey&field1=13.0";
// Load SSL certificate (root CA)
var certificate = Resources.GetBytes(Resources.BinaryResources.thingspeakCertificate);
var certx509 = new[] { new X509Certificate(certificate) };
request = HttpWebRequest.Create(url) as HttpWebRequest;
request.HttpsAuthentCerts = certx509;
request.KeepAlive = false;
request.Method = "GET";
request.ReadWriteTimeout = 2000;
using var response2 = request.GetResponse() as HttpWebResponse;
if (response2 != null)
{
Debug.WriteLine(response2.StatusCode == HttpStatusCode.OK
? "Data successfully sent to ThingSpeak (https)"
: $"Failed to send data: {response2.StatusCode}");
}
}
catch (Exception e)
{
Debug.WriteLine(“Exception” + e.Message);
}