Json in .NETMF

I am working on data upload to server.

I will use json string with a “PUT” method. The server side is working and tested with GPRS module.

I will make an Ethernet solution with the ENC28 module.

I am trying to upload data, but an Exception´s break the program.

Anyone who has experience with Json in .NETMF or sample code.

I want to receive and transmit a Json string to/from server.


        public static void senddata()
        {
            var webAddr = "http:/****.com/datapoint";  

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);

            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.ContentLength = json.Length;
            httpWebRequest.Method = "PUT";

            Debug.Print("webAddr: " + webAddr + 
"\nhttpWebRequest: " + httpWebRequest + 
"\nhttpWebRequest.ContentType: " + httpWebRequest.ContentType + 
"\nhttpWebRequest.ContentLength: " + httpWebRequest.ContentLength + 
"\nhttpWebRequest.Method: " + httpWebRequest.Method + "\n\n");

            try
            {
                Debug.Print("Start: StreamWriter(httpWebRequest.GetRequestStream())");
      using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))  //<== BREAK!!!
                {
                    string json = "Json sting with datavalue";

                    streamWriter.Write(json);
                    streamWriter.Flush();
                    Debug.Print("Flush done!!");
                }

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    Debug.Print("modtaget er " + result);
                }

                Debug.Print("Status OK");
            }
            catch
            {
                Debug.Print("Error i send data");
            }
           
        }


OUTPUT:

IP address is set.
webAddr: http://*** /DataPoint
httpWebRequest: System.Net.HttpWebRequest
httpWebRequest.ContentType: application/json; charset=utf-8
httpWebRequest.ContentLength: 128
httpWebRequest.Method: PUT

Start: StreamWriter(httpWebRequest.GetRequestStream())
#### 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: 0019] ####
#### System.Net.HttpWebRequest::GetRequestStream [IP: 0008] ####
#### CleanEthernet.Program::Main [IP: 0017] ####
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060
#### Exception System.Net.WebException - 0x00000000 (1) ####
#### Message: host not available
#### System.Net.HttpWebRequest::EstablishConnection [IP: 00f1] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
#### System.Net.HttpWebRequest::GetRequestStream [IP: 0008] ####
#### CleanEthernet.Program::Main [IP: 0017] ####
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll

The error isn’t related to JSON data.

It seems that there is a problem with host name resolving.

Try a simple test using only Dns.GetHostEntry() to resolve host name without HTTP just to verify if it is the problem.

Paolo.

@ ppatierno

thanks for fast reply

Dns.GetHostEntry(“local pc IP”); <= works

Dns.GetHostEntry(“to server”); I don´t have access to any ip. only the domain

You can provide host name to Dns.GetHostEntry() not necessarily IP address.

look like a time out.

Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1)

#### Message: 
#### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
#### System.Net.Dns::GetHostEntry [IP: 0008] ####
#### CleanEthernet.MyHttpClient::senddata [IP: 0091] ####
#### CleanEthernet.Program::Main [IP: 0017] ####
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060

A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060

Are you sure that the server is reachable ?

Try from your PC.

Can you solve with Dns.GetHostEntry() e more common host name like … www.microsoft.com, www.ghielectronics.com :slight_smile: ?

Same error

I try to change the network router. I will write back when this is done.

Same error.

I have tried with Ethernet cable from PC. Not any relpy from any WWW.

it may be a firewall issue?

from your PC can you ping some domain ? The DNS solves the host name ?

yes i can ping google.com with cmd with good respons

Done, its working. i made a stupid error with dhcp.

thanks for your help ;D

You are welcome.

Paolo.