Http client not working

The example http client is not working for me.

https://docs.ghielectronics.com/software/tinyclr/tutorials/http-https.html

#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (1) ####
    #### Message: 
    #### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::GetHostByName [IP: 0000] ####
    #### System.Net.Dns::GetHostEntry [IP: 000b] ####
    #### System.Net.HttpWebRequest::EstablishConnection [IP: 00ed] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### SitCoreDev.Program::TestHttp [IP: 0034] ####
    #### SitCoreDev.Program::Main [IP: 01a7] ####
    #### Exception System.Net.WebException - 0x00000000 (1) ####
    #### Message: 
    #### System.Net.HttpWebRequest::GetResponse [IP: 00d3] ####
    #### SitCoreDev.Program::TestHttp [IP: 0034] ####
    #### SitCoreDev.Program::Main [IP: 01a7] ####
Exception thrown: 'System.Net.WebException' in GHIElectronics.TinyCLR.Networking.Http.dll

I can confirm in my router that the Wifi 7 click has connected to the network.

Just quick tested, look to me no problem. The test used wifi also.

image

Could you please post entire code since the doc just show a piece of it?

using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Devices.Network;
using GHIElectronics.TinyCLR.Devices.Spi;
using GHIElectronics.TinyCLR.Drivers.Microchip.Winc15x0;
using GHIElectronics.TinyCLR.Networking.Mqtt;
using GHIElectronics.TinyCLR.Pins;
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;

namespace SitCoreDev
{
    class Program
    {
        static void Main()
        {

            var enablePin = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PI0);
            enablePin.SetDriveMode(GpioPinDriveMode.Output);
            enablePin.Write(GpioPinValue.High);

            SpiNetworkCommunicationInterfaceSettings netInterfaceSettings =
                new SpiNetworkCommunicationInterfaceSettings();

            var cs = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PG12);

            var settings = new SpiConnectionSettings()
            {
                ChipSelectLine = cs,
                ClockFrequency = 4000000,
                Mode = SpiMode.Mode0,
                ChipSelectType = SpiChipSelectType.Gpio,
                ChipSelectHoldTime = TimeSpan.FromTicks(10),
                ChipSelectSetupTime = TimeSpan.FromTicks(10)
            };

            netInterfaceSettings.SpiApiName = SC20260.SpiBus.Spi3;

            netInterfaceSettings.GpioApiName = SC20260.GpioPin.Id;

            netInterfaceSettings.SpiSettings = settings;
            netInterfaceSettings.InterruptPin = GpioController.GetDefault().
                OpenPin(SC20260.GpioPin.PG6);

            netInterfaceSettings.InterruptEdge = GpioPinEdge.FallingEdge;
            netInterfaceSettings.InterruptDriveMode = GpioPinDriveMode.InputPullUp;
            netInterfaceSettings.ResetPin = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PI8);
            netInterfaceSettings.ResetActiveState = GpioPinValue.Low;

            var networkController = NetworkController.FromName
                ("GHIElectronics.TinyCLR.NativeApis.ATWINC15xx.NetworkController");

            WiFiNetworkInterfaceSettings wifiSettings = new WiFiNetworkInterfaceSettings()
            {
                Ssid = "PecnikVip",
                Password = "PecnikVip",
            };

            //    wifiSettings.Address = new IPAddress(new byte[] { 192, 168, 0, 122 });
            //    wifiSettings.SubnetMask = new IPAddress(new byte[] { 255, 255, 255, 0 });
            //    wifiSettings.GatewayAddress = new IPAddress(new byte[] { 192, 168, 0, 1 });
            //    wifiSettings.DnsAddresses = new IPAddress[] { new IPAddress(new byte[]
            //{ 75, 75, 75, 75 }), new IPAddress(new byte[] { 75, 75, 75, 76 }) };

            wifiSettings.MacAddress = new byte[] { 0x00, 0x4, 0x00, 0x00, 0x00, 0x00 };
            wifiSettings.IsDhcpEnabled = true;
            wifiSettings.IsDynamicDnsEnabled = true;
            wifiSettings.TlsEntropy = new byte[] { 0, 1, 2, 3 };

            networkController.SetInterfaceSettings(wifiSettings);
            networkController.SetCommunicationInterfaceSettings(netInterfaceSettings);
            networkController.SetAsDefaultController();

            networkController.NetworkAddressChanged += NetworkController_NetworkAddressChanged;

            networkController.NetworkLinkConnectedChanged += NetworkController_NetworkLinkConnectedChanged;

            networkController.Enable();

            Thread.Sleep(2000);

            //Scan for WiFi access points:
            string[] ssidList = Winc15x0Interface.Scan();

            foreach (string item in ssidList)
            {
                Debug.WriteLine(item);
            }

            //Get Relative Signal Strength Indicator for the connected access point:
            int signalStrength = Winc15x0Interface.GetRssi();

            Debug.WriteLine(signalStrength.ToString());

            //Get the WiFi module's MAC address:
            byte[] macAddress = Winc15x0Interface.GetMacAddress();

            Debug.WriteLine(macAddress.ToString());

            TestHttp();

            // Network is ready to used
            Thread.Sleep(Timeout.Infinite);

        }

        static void TestHttp()
        {
            var url = "http://www.bing.com/robots.txt";

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

            try
            {
                using (var req = HttpWebRequest.Create(url) as HttpWebRequest)
                {
                    req.KeepAlive = false;
                    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);

                                String page = "";

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

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

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

            }
        }

        private static void NetworkController_NetworkLinkConnectedChanged
    (NetworkController sender, NetworkLinkConnectedChangedEventArgs e)
        {
            // Raise event connect/disconnect
        }

        private static void NetworkController_NetworkAddressChanged
            (NetworkController sender, NetworkAddressChangedEventArgs e)
        {
            var ipProperties = sender.GetIPProperties();
            var address = ipProperties.Address.GetAddressBytes();
            Debug.WriteLine("IP: " + address[0] + "." + address[1] + "." + address[2] +
                "." + address[3]);
        }
    }
}

exception:

The thread '<No Name>' (0x2) has exited with code 0 (0x0).
IP: 0.0.0.0
IP: 192.168.0.113
PecnikVip
76
System.Byte[]
    #### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (1) ####
    #### Message: 
    #### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::GetHostByName [IP: 0000] ####
    #### System.Net.Dns::GetHostEntry [IP: 000b] ####
    #### System.Net.HttpWebRequest::EstablishConnection [IP: 00ed] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### SitCoreDev.Program::TestHttp [IP: 0034] ####
    #### SitCoreDev.Program::Main [IP: 0178] ####
    #### Exception System.Net.WebException - 0x00000000 (1) ####
    #### Message: 
    #### System.Net.HttpWebRequest::GetResponse [IP: 00d3] ####
    #### SitCoreDev.Program::TestHttp [IP: 0034] ####
    #### SitCoreDev.Program::Main [IP: 0178] ####
Exception thrown: 'System.Net.WebException' in GHIElectronics.TinyCLR.Networking.Http.dll

As you can see it finds PecnikVip WIFI, signal strength is 76, IP address etc…
And i can confirm in my router that the device has connected.

Can you remove Scan and GetRssi ?

It works intermittently, i get this often:

#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (1) ####
    #### Message: 
    #### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::GetRemoteAddress [IP: 0000] ####
    #### System.Net.Sockets.Socket::GetEndPoint [IP: 003d] ####
    #### System.Net.Sockets.Socket::get_RemoteEndPoint [IP: 0005] ####
    #### System.Net.Sockets.NetworkStream::.ctor [IP: 0006] ####
    #### System.Net.HttpWebRequest::EstablishConnection [IP: 0195] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### SitCoreDev.Program::TestHttp [IP: 0034] ####
    #### SitCoreDev.Program::Main [IP: 0130] ####
    #### Exception System.IO.IOException - 0x00000000 (1) ####
    #### Message: 103
    #### System.Net.Sockets.NetworkStream::.ctor [IP: 0032] ####
    #### System.Net.Sockets.NetworkStream::.ctor [IP: 0006] ####
    #### System.Net.HttpWebRequest::EstablishConnection [IP: 0195] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### SitCoreDev.Program::TestHttp [IP: 0034] ####
    #### SitCoreDev.Program::Main [IP: 0130] ####
    #### Exception System.Net.WebException - 0x00000000 (1) ####
    #### Message: 
    #### System.Net.HttpWebRequest::GetResponse [IP: 00d3] ####
    #### SitCoreDev.Program::TestHttp [IP: 0034] ####
    #### SitCoreDev.Program::Main [IP: 0130] ####
Exception thrown: 'System.Net.WebException' in GHIElectronics.TinyCLR.Networking.Http.dll

Ok, it appears to work now, but you should still fix the issue.

Could you please tell us how you got it work first then we know what issue is?

Removed scan, rssi and GetMacAddress and it works now, something from the Winc15x0Interface is crashing it.
Also i don’t test from main event but from a button click. It needs more time to initialize or?

is it release version or still preview?

TinyCLR v2.0.0.8000 (RTW) running on SC20100 Dev Rev A
GetHostByName still fails more then succeeds.

Here’s another one:

The thread '<No Name>' (0x2) has exited with code 0 (0x0).
#### Exception System.Net.Sockets.SocketException - 0x00000000 (3) ####
#### Message: 
#### System.Net.InputNetworkStreamWrapper::Read_HTTP_Line [IP: 0157] ####
#### System.Net.HttpWebRequest::ParseHTTPResponse [IP: 002e] ####
#### System.Net.HttpWebRequest::GetResponse [IP: 0035] ####
#### SitCoreDev.Program::TestApi [IP: 0034] ####
#### SitCoreDev.Program::Btn_ValueChanged [IP: 0010] ####
#### GHIElectronics.TinyCLR.Devices.Gpio.GpioPin::OnValueChanged [IP: 000e] ####
#### GHIElectronics.TinyCLR.Devices.Gpio.Provider.GpioControllerApiWrapper::OnDispatcher [IP: 0047] ####
#### Exception System.Net.WebException - 0x00000000 (3) ####
#### Message: 
#### System.Net.HttpWebRequest::GetResponse [IP: 00c8] ####
#### SitCoreDev.Program::TestApi [IP: 0034] ####
#### SitCoreDev.Program::Btn_ValueChanged [IP: 0010] ####
#### GHIElectronics.TinyCLR.Devices.Gpio.GpioPin::OnValueChanged [IP: 000e] ####
#### GHIElectronics.TinyCLR.Devices.Gpio.Provider.GpioControllerApiWrapper::OnDispatcher [IP: 0047] ####

Exception thrown: ‘System.Net.WebException’ in GHIElectronics.TinyCLR.Networking.Http.dll

The first issue related to https://github.com/ghi-electronics/TinyCLR-Libraries/issues/646. We decided won’t fix for now, unless show us the reason to fix.

You call them before Enable() should be fine.

For second exception, we need more information. Is it random? any simple project?