Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Simple HTTP Client?

internet http httpclient network socket tcpip

  • Please log in to reply
4 replies to this topic

#1 Chuckles

Chuckles

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

Posted 08 November 2013 - 01:15 AM

There is a remote web server waiting to accept simple HTTP GET traffic and I want to make calls to it from my Netduino Plus 2. I am familar with using WebClient in the .NET Framework, but do not see anything comparable for the Micro Framework.

 

What is the proper way to just send a GET from a Netduino? I just want to send stuff like

 

http://192.168.1.1/g...a=1&b=2&c=3&d=4

 

from the Netduino to my remote web server.

 

 

Thanks

 

.



#2 qsnapper

qsnapper

    New Member

  • Members
  • Pip
  • 8 posts

Posted 08 November 2013 - 05:39 PM

have exactly the same question! 



#3 qsnapper

qsnapper

    New Member

  • Members
  • Pip
  • 8 posts

Posted 09 November 2013 - 12:37 PM

lot of digging around found some sample code that works for me, hopefully works for you too.

Be sure to add the 'System.Http' reference. For some reason this doesn't work for HTTPS URL's though

using System;using System.IO;using System.Text;using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;namespace NetduinoApplication2{    public class Program    {        public static void Main()        {            // write your code here            // produce request            var requestUri = "http://192.168.1.1/?a=1";            Debug.Print(requestUri);            using (var request = (HttpWebRequest)WebRequest.Create(requestUri))            {                request.Method = "GET";                // headers                request.Headers.Add("X-ApiKey", "ThisIsMyKey");                // send request and receive response                using (var response = (HttpWebResponse)request.GetResponse())                {                    // consume response                    HandleResponse(response);                }            }        }        public static void HandleResponse(HttpWebResponse response)        {            // response status line            Debug.Print("HTTP/" + response.ProtocolVersion + " " +                        response.StatusCode + " " +                        response.StatusDescription);            // response headers            string[] headers = response.Headers.AllKeys;            foreach (string name in headers)            {                Debug.Print(name + ": " + response.Headers[name]);            }            // response body            var buffer = new byte[(int)response.ContentLength];            Stream stream = response.GetResponseStream();            int toRead = buffer.Length;            while (toRead > 0)            {                // already read: buffer.Length - toRead                int read = stream.Read(buffer, buffer.Length - toRead, toRead);                toRead = toRead - read;            }            char[] chars = Encoding.UTF8.GetChars(buffer);            Debug.Print(new string(chars));        }    }}


#4 qsnapper

qsnapper

    New Member

  • Members
  • Pip
  • 8 posts

Posted 09 November 2013 - 01:18 PM

The API I am trying to work with only allows HTTPS GET requests.

Can someone help me out how I could change the above to get it to work with an HTTPS URL.

 

Thanks



#5 Chuckles

Chuckles

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

Posted 15 November 2013 - 02:03 PM

@qsnapper, thanks for providing input. I was unable to get that to work with my configuration for some reason. I did not have the right reference assemblies loaded up to get those WebClient bits to work.

 

I used  Toolbox.NETMF.NET.HTTP_Client this way:

 var httpClient = new HTTP_Client( new IntegratedSocket( _remoteCollectionSite, 80 ) );                fullUri = _remoteCollectionPath + _paramBuilder.ToString();                HTTP_Client.HTTP_Response httpResponse = httpClient.Get( fullUri  );                Debug.Print( _remoteCollectionSite + fullUri );                Debug.Print( httpResponse.ToString() + "n");

However, if the remote server is not available the exception that comes from a timeout is not easy to trap. I might be having a problem with how Exceptions are caught in my catch{} block because I never get details as I am used to when creating .NET (non Micro) Framework software.

 

Thanks.







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.