Main Site Documentation

ChipworkX Development System WebRequest error [Closed]


#1

Hi . I’m new with MF so maybe its a stupid question. I have found several similar posts and different solutions but they don’t seem to work for me.
I have ChipworkX Development System and I’m trying to sent a WebRequest .

My code looks like this, sipmle:


namespace MFHTTPTest
{
    public class Program
    {
        public static void Main()
        {
            WebRequest request = WebRequest.Create(
              "http://www.google.com");
            WebResponse response = request.GetResponse();
            Debug.Print(((HttpWebResponse)response).StatusDescription);
            response.Close();
        }
    }
}

I’m getting exception:


 #### Exception System.Exception - 0x00000000 (1) ####
    #### Message: DNS server IP address was not found.
    #### GHIElectronics.NETMF.Net.Dns::GetHostEntry [IP: 0038] ####
    #### GHIElectronics.NETMF.Net.HttpWebRequest::EstablishConnection [IP: 00e1] ####
    #### GHIElectronics.NETMF.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
    #### GHIElectronics.NETMF.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### MFHTTPTest.Program::Main [IP: 000c] ####

But when I this code, that i found as example :


public class Program
    {
        static public bool ethernet_event = false;
        static public bool ethernet_last_status = false;
        static public bool network_is_read = false;
        static public ManualResetEvent NetworkAvailablityBlocking = null;
        public static void Main()
        {

            if (!Ethernet.IsEnabled)
            {
                Ethernet.Enable();
            }
            NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
            NetworkInterface[] netif = NetworkInterface.GetAllNetworkInterfaces();
            // Ethernet is always the first interface netif[0]
            NetworkAvailablityBlocking = new ManualResetEvent(false);
            if (!Ethernet.IsCableConnected)
            {
                Debug.Print("Cable is not connected!");
                NetworkAvailablityBlocking.Reset();
                while (!NetworkAvailablityBlocking.WaitOne(5000, false))
                {
                    if (!Ethernet.IsCableConnected)
                    {
                        Debug.Print("Cable is not connected!");
                        Debug.Print("Still waiting.");
                    }
                    else
                        break;
                }
            }

            Debug.Print("Ethernet cable is connected!");
            Debug.Print("Enable DHCP");
            try
            {
                if (!netif[0].IsDhcpEnabled)
                    netif[0].EnableDhcp();// This function is blocking
                else
                {
                    netif[0].RenewDhcpLease();// This function is blocking
                }
                network_is_read = true;
                Debug.Print("Network settings:");
                Debug.Print("IP Address: " + netif[0].IPAddress);
                Debug.Print("Subnet Mask: " + netif[0].SubnetMask);
                Debug.Print("Default Getway: " + netif[0].GatewayAddress);
                Debug.Print("DNS Server: " + netif[0].DnsAddresses[0]);

            }
            catch
            {
                Debug.Print("DHCP Faild");
            }

            Debug.Print("Test DNS");
            try
            {
                IPHostEntry myIP = Dns.GetHostEntry("www.google.com");



                if (myIP != null)
                {
                    Debug.Print(myIP.HostName + ": " + myIP.AddressList[0].ToString());
                }
            }
            catch
            {
                Debug.Print("Faild to Get the host entry of the FQN from DNS server!");
            }

            Thread.Sleep(Timeout.Infinite);

        }

        static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            if (e.IsAvailable)
            {
                if (Ethernet.IsCableConnected)
                {
                    if (ethernet_last_status != true)
                    {
                        ethernet_last_status = true;
                        NetworkAvailablityBlocking.Set();
                    }
                }
            }
            else
            {
                if (!WiFi.IsLinkConnected)
                {
                    if (ethernet_last_status != false)
                    {
                        ethernet_last_status = false;
                        network_is_read = false;
                    }
                }
            }
        }

    }

I’m getting successful response:


Ethernet cable is connected!
Enable DHCP
Network settings:
IP Address: 192.168.1.245
Subnet Mask: 255.255.255.0
Default Getway: 192.168.1.200
DNS Server: 87.253.32.130
Test DNS
www.google.com: 173.194.113.177

What can be the problem? Thanks In advance.


#2

Welcome to the forum.

The code example that works is initializing the Ethernet port. Your code does not initialize the Ethernet port.

The solution is to include the initialization procedure, from the code that works, and then make the Web request.