FEZ Cobra and WebHttpRequest

I’m having problem with my code. Below is the code. I’m trying connect to the server.

My code:
using System;
using Microsoft.SPOT;
using System.Xml;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net;
using Microsoft.SPOT.IO;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.thingspeak.com/update?key={mykey}&field1=10");
        request.KeepAlive = true;
        try
        {
            HttpWebResponse result = (HttpWebResponse)request.GetResponse();

            Stream respStream = result.GetResponseStream();
        }
        catch (Exception ex)
        {
            Debug.Print(ex.Message);
        }

And error message:

The thread ‘’ (0x2) has exited with code 0 (0x0).
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
#### System.Net.Dns::GetHostEntry [IP: 0008] ####
#### System.Net.HttpWebRequest::EstablishConnection [IP: 00e1] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
#### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
#### MFWindowApplication1.Weather::.ctor [IP: 001b] ####
#### MFWindowApplication1.Desktop::.ctor [IP: 0066] ####
#### MFWindowApplication1.Program::Main [IP: 0028] ####
#### SocketException ErrorCode = -1
#### SocketException ErrorCode = -1
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = -1
#### SocketException ErrorCode = -1
#### Exception System.Net.WebException - 0x00000000 (1) ####
#### Message: host not available
#### System.Net.HttpWebRequest::EstablishConnection [IP: 00f1] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
#### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
#### MFWindowApplication1.Weather::.ctor [IP: 001b] ####
#### MFWindowApplication1.Desktop::.ctor [IP: 0066] ####
#### MFWindowApplication1.Program::Main [IP: 0028] ####
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll
#### Exception System.Net.WebException - 0x00000000 (1) ####
#### Message:
#### System.Net.HttpWebRequest::GetResponse [IP: 00d3] ####
#### MFWindowApplication1.Weather::.ctor [IP: 001b] ####
#### MFWindowApplication1.Desktop::.ctor [IP: 0066] ####
#### MFWindowApplication1.Program::Main [IP: 0028] ####
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll

It seems like the FEZ isn’t able to reslove the host address.

Are you using DHCP? Is the DNS server correct in your network?

Did you try the httpclient sample program from Microsoft in \my documents\Microsoft .NET Micro Framework 4.1\Samples\HttpClient\HttpClient ?

Here is the code. This is usualy a good start point.
I haven’t tested it so far, though… :wink:


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Microsoft Corporation.  All rights reserved.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Net.Security;

/// This program demonstrates how to use the .NET Micro Framework HTTP classes 
/// to create a simple HTTP client that retrieves pages from several different 
/// websites, including secure sites.
namespace HttpClientSample
{
    public static class MyHttpClient
    {
        /// <summary>
        /// Retrieves a page from a Web server, using a simple GET request.
        /// </summary>
        public static void Main()
        {
            // Root CA Certificate needed to validate HTTPS servers.
            byte[] ca = Resource1.GetBytes(
                Resource1.BinaryResources.VerisignCA);

            X509Certificate[] caCerts = 
                new X509Certificate[] { new X509Certificate(ca) };

            // Initialize the default webproxy to be used by all 
            // HttpWebRequests.
            // Change the proxy address to fit your environment.
            HttpWebRequest.DefaultWebProxy = 
                new WebProxy("itgproxy.dns.microsoft.com", true);

            // Print the HTTP data from each of the following pages.
            PrintHttpData("http://autos.msn.com/default.aspx", null);
            PrintHttpData("http://www.nytimes.com/", null);

            // Test SSL connection with no certificate verification
            PrintHttpData("https://www.google.com/accounts/ManageAccount/", null);

            // Read from secure webpages by using the Verisign Root CA 
            // certificate that is stored in the Resource1.resx file.
            PrintHttpData("https://www.google.com/accounts/ManageAccount/", 
                caCerts);
        }

        /// <summary>
        /// Prints the HTTP Web page from the given URL and status data while 
        /// receiving the page.
        /// </summary>
        /// <param name="url">The URL of the page to print.</param>
        /// <param name="caCerts">The root CA certificates that are required for 
        /// validating a secure website (HTTPS).</param>
        public static void PrintHttpData(string url, X509Certificate[] caCerts)
        {
            // Create an HTTP Web request.
            HttpWebRequest request = 
                HttpWebRequest.Create(url) as HttpWebRequest;

            // Assign the certificates. The value must not be null if the
            // connection is HTTPS.
            request.HttpsAuthentCerts = caCerts;

            // Set request.KeepAlive to use a persistent connection. 
            request.KeepAlive = true;

            // Get a response from the server.
            WebResponse resp = null;

            try
            {
                resp = request.GetResponse();
            }
            catch (Exception e)
            {
                Debug.Print("Exception in HttpWebRequest.GetResponse(): " + 
                    e.ToString());
            }

            // Get the network response stream to read the page data.
            if (resp != null)
            {
                Stream respStream = resp.GetResponseStream();
                string page = null;
                byte[] byteData = new byte[4096];
                char[] charData = new char[4096];
                int bytesRead = 0;
                Decoder UTF8decoder = System.Text.Encoding.UTF8.GetDecoder();
                int totalBytes = 0;

                // allow 5 seconds for reading the stream
                respStream.ReadTimeout = 5000; 

                // If we know the content length, read exactly that amount of 
                // data; otherwise, read until there is nothing left to read.
                if (resp.ContentLength != -1)
                {
                    for (int dataRem = (int)resp.ContentLength; dataRem > 0; )
                    {
                        Thread.Sleep(500);
                        bytesRead = 
                            respStream.Read(byteData, 0, byteData.Length);
                        if (bytesRead == 0)
                        {
                            Debug.Print("Error: Received " +
                                (resp.ContentLength - dataRem) + " Out of " +
                                resp.ContentLength);
                            break;
                        }
                        dataRem -= bytesRead;

                        // Convert from bytes to chars, and add to the page 
                        // string.
                        int byteUsed, charUsed;
                        bool completed = false;
                        totalBytes += bytesRead;
                        UTF8decoder.Convert(byteData, 0, bytesRead, charData, 0, 
                            bytesRead, true, out byteUsed, out charUsed, 
                            out completed);
                        page = page + new String(charData, 0, charUsed);

                        // Display the page download status.
                        Debug.Print("Bytes Read Now: " + bytesRead + 
                            " Total: " + totalBytes);
                    }

                    page = new String(
                        System.Text.Encoding.UTF8.GetChars(byteData));
                }
                else
                {
                    // Read until the end of the data is reached.
                    while (true)
                    {
                        // If the Read method times out, it throws an exception, 
                        // which is expected for Keep-Alive streams because the 
                        // connection isn't terminated.
                        try
                        {
                            Thread.Sleep(500);
                            bytesRead = 
                                respStream.Read(byteData, 0, byteData.Length);
                        }
                        catch (Exception)
                        {
                            bytesRead = 0;
                        }

                        // Zero bytes indicates the connection has been closed 
                        // by the server.
                        if (bytesRead == 0)
                        {
                            break;
                        }

                        int byteUsed, charUsed;
                        bool completed = false;
                        totalBytes += bytesRead;
                        UTF8decoder.Convert(byteData, 0, bytesRead, charData, 0, 
                            bytesRead, true, out byteUsed, out charUsed, 
                            out completed);
                        page = page + new String(charData, 0, charUsed);

                        // Display page download status.
                        Debug.Print("Bytes Read Now: " + bytesRead + 
                            " Total: " + totalBytes);
                    }

                    Debug.Print("Total bytes downloaded in message body : " 
                        + totalBytes);
                }

                // Display the page results.
                Debug.Print(page);

                // Close the response stream.  For Keep-Alive streams, the 
                // stream will remain open and will be pushed into the unused 
                // stream list.
                resp.Close();
            }
        }
    }
}

Hi, I tried run the sample but I got the same exception.
Method ‘PrintHttpData’, line: resp = request.GetResponse();

I use DHCP but also tried used static address with DNS: 4.4.4.4 (I found this value somewhere. I don’t know if it’s correct).

4.4.4.4 is not a valid DNS.
Try either 8.8.8.8 or 8.8.4.4 (google public DNS)

You problem probably comes from lack of correct DNS resolution.

If you have a double about it, you can still try to make a request to site with an ip address, without a hostname :slight_smile:

It’s much better if I decided to use IP addresses but every time response from the server is like : ‘302 document moved’. The same address entered in the web browser working ok.

The solution is to enter DNS 4.2.2.2 :slight_smile: