Getting errors building DoTestHttps()

I am trying build the sample code from https://docs.ghielectronics.com/software/tinyclr/tutorials/tls.html.

I did add the nuget package GHIElectronics.TinyCLR.Networking.Http. The package shows up in the references.
I added this line at the top:

using GHIElectronics.TinyCLR.Networking.Http;

However I am getting build error like below.

|Error|CS0234|The type or namespace name 'Http' does not exist in the namespace 'GHIElectronics.TinyCLR.Networking' (are you missing an assembly reference?)|TinyCLRApplication|C:\Users\purel\source\repos\hololite\TinyCLR\apps\TinyCLRApplication\HttpsTest.cs|8|Active|

For some reasons, it does not recognize the GHIElectronics.TinyCLR.Networking.Http namespace.

Here is the complete code:
using System.Diagnostics;
using GHIElectronics.TinyCLR.Devices.Network;
using GHIElectronics.TinyCLR.Pins;
using GHIElectronics.TinyCLR.Networking;
using GHIElectronics.TinyCLR.Networking.Http;

namespace TinyCLRApplication1 {

    class HttpTest {

        static void DoTestHttps()
        {
            var url = "https://www.google.com";

            var certificates = Resources.GetBytes(Properties.Resources.BinaryResources.GlobalSign);

            X509Certificate[] certx509 = new X509Certificate[] { new X509Certificate(certificates) };

            int read = 0, total = 0;
            byte[] result = new byte[512];

            try
            {
                using (var req = HttpWebRequest.Create(url) as HttpWebRequest)
                {
                    req.KeepAlive = false;
                    req.HttpsAuthentCerts = certx509;
                    req.ReadWriteTimeout = 2000;

                    using (var res = req.GetResponse() as HttpWebResponse)
                    {
                        using (var stream = res.GetResponseStream())
                        {
                            do
                            {
                                read = stream.Read(result, 0, result.Length);
                                total += read;

                                System.Diagnostics.Debug.WriteLine("read : " + read);
                                System.Diagnostics.Debug.WriteLine("total : " + total);

                                var page = new String(System.Text.Encoding.UTF8.
                                    GetChars(result, 0, read));

                                System.Diagnostics.Debug.WriteLine("Response : " + page);
                            }

                            while (read != 0);
                        }
                    }
                }
            }

            catch { }
        }
    }
}

The assembly GHIElectronics.TinyCLR.Networking.Http provides System.Net namespace. So you should be Using System.Net and not the TinyCLR.Networking ones.

Something like this :

using System.Net;
using System.Security.Cryptography.X509Certificates; 

This does compile without error.
I don’t have certificates handy so I can’t get further, though.

1 Like

Ok will give it a try. I should have looked at the source code of this assembly. Thanks!

HttpWebRequest or WebRequest need System.Net.

Just add

using System.Net;
1 Like

Now getting errors on Resources and Properties types.

var certificates = Resources.GetBytes(Properties.Resources.BinaryResources.GlobalSign);

Are these types in the class library? What is the namespace?
I understand I need to download the cert and stick it somewhere in the assembly’s resources.

Thanks

  • you have to add resource certificate to your project.
  • Save it.
  • Point to that error, right click and select “Quick Action and Refactoring…”, select :
using your_project.Properties;

I added the cer file into the project’s resources.

‘Quick Action and Refactoring’ gives me 3 options to fix it: system.resource, generate var, or generate type. Which one should I choose?

Add this one on top

using TinyCLRApplication1.Properties;

Or move mouse to Resources.GetByte(…)

It does not recognize ‘Properties’. Here is the snapshot.

You can’t just add your resource to Resource folder, below is common way:

You have to save all (not just save).

would this shelp?

http://docs.ghielectronics.com/software/tinyclr/tutorials/resources.html

I added it via the VS add resource menu. VS copied the res file into the folder for me.

Great, this looks very useful. Looking into it now. Thanks.

Turned out I was doing the same step as http://docs.ghielectronics.com/software/tinyclr/tutorials/resources.html for adding the file resource.

Still getting the errors.

I see you have Resource1 and Resource2. Try with these.

Or better, do it from a new project.

I agree. Start a new project if this is new to you.

Started a new project. Replaced ‘Resources’ with ‘Resource1’.
Now it solved the Resource issue but still getting errors on ‘Properties’.

“Properties” come from your project, here is HttpsTest.

Full will be: HttpsTest.Properties.Resource1.BinaryResources.GlobalSign);

I have the following:

certificates = Resource1.GetBytes(HttpsTest.Properties.Resources.BinaryResources.GlobalSign);

But still getting error below:

|Error|CS0234|The type or namespace name 'Properties' does not exist in the namespace 'HttpsTest' (are you missing an assembly reference?)|HttpsTest|C:\Users\purel\source\repos\hololite\TinyCLR\apps\HttpsTest\Program.cs|19|Active|

This is the correct one:

var certificates = Resource1.GetBytes(Resource1.BinaryResources.global);

Thanks guys for the help.
I suggest the fix is updated on the sample codes.