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

System.Net.Web exception in request.GetRequestStream()

exception GetRequestStream() System.Net.Web exception

Best Answer Chris Walker, 14 January 2015 - 07:32 AM

Hi Sambeet,

Can you access the service via HTTP instead of HTTPS? There's no support for SSL on Netduino Plus 2 in today's firmware.

Also, you may want to add a "check to make sure my board has an Internet address" bit of code up top if you're using DHCP...in case it takes a few seconds to obtain and resolve an IP address from your router.

Chris Go to the full post


  • Please log in to reply
2 replies to this topic

#1 Sambeet Panigrahi

Sambeet Panigrahi

    New Member

  • Members
  • Pip
  • 3 posts

Posted 11 January 2015 - 09:02 PM

Hi

 

I am a newbie trying to just update my switch state to Thingspeak,but each time i run my code,the program throws a System.Net.WebException on the GetRequestStream() which is going on happening.Below is my code:

 

 
///Authored by-: Sambeet Panigrahi
///Dated on-:10th January 2015

/*This program updates the Netduino Plus switch state to Thingspeak*/
using System;
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;
using System.Text;
using System.IO;


namespace LED__control_Thingspeak
{
    public class Program
    {
        public static void Main()
        {
            var SwitchPort = new InputPort(Pins.ONBOARD_BTN, false, Port.ResistorMode.
                Disabled);                                                           //Open the switchport

            bool onned = SwitchPort.Read();//Read the status of switchport

            if (onned)
            {
                update_thingspeak("30");
            }
            else
            {
                update_thingspeak("60");
            }

        }//Main ends

        static void update_thingspeak(string value)
        {
            //Create a http response object
                WebRequest request = HttpWebRequest.Create("https://api.thingspeak.com/update");
            //Setting the request method
                request.Method = "POST";
            //The data to be sent
                string data = "api_key=" + "T5Q1Y9QBY1TULCBG" + "&field1=" + value;
            //Creating the byte array
                byte[] bytearray = Encoding.UTF8.GetBytes(data);
            //Setting the MIME
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = bytearray.Length;
            //Opening the datastream
                Stream datastream = request.GetRequestStream();
            //Writing to the datastream
                datastream.Write(bytearray, 0, bytearray.Length);
            //Closing the stream
                datastream.Close();
            

            //Obtaining a response
                WebResponse response = request.GetResponse();//Creating a response
                datastream = response.GetResponseStream();//Getting the response stream
                StreamReader reader = new StreamReader(datastream);// Read the content.
                string responseFromServer = reader.ReadToEnd();// Display the content.
                Debug.Print(responseFromServer);
            
        }//update_thingspeak ends
    }//class program ends
}//namespace ends

 

            Can someone help me? I am a little out of time.


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 14 January 2015 - 07:32 AM   Best Answer

Hi Sambeet,

Can you access the service via HTTP instead of HTTPS? There's no support for SSL on Netduino Plus 2 in today's firmware.

Also, you may want to add a "check to make sure my board has an Internet address" bit of code up top if you're using DHCP...in case it takes a few seconds to obtain and resolve an IP address from your router.

Chris

#3 Sambeet Panigrahi

Sambeet Panigrahi

    New Member

  • Members
  • Pip
  • 3 posts

Posted 18 January 2015 - 01:30 PM

Thanks! The code works if i just give the numeric IP address instead of "https://api.thingspeak.com/update" in the code.

 







Also tagged with one or more of these keywords: exception, GetRequestStream(), System.Net.Web exception

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.