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

Error(?) using WebRequest


  • Please log in to reply
3 replies to this topic

#1 Larre

Larre

    Member

  • Members
  • PipPip
  • 11 posts

Posted 01 January 2014 - 08:48 AM

Hello all,

 

I am trying to do a WebRequest from my Netduino to poll a string from another system. I am using the code below to perform the request:

 public double GetTemperature(string address)        {            var request = WebRequest.Create(_url);            var response = request.GetResponse();            request.Dispose();            var sr = new StreamReader(response.GetResponseStream());            var source = sr.ReadToEnd();            response.Dispose();            sr.Dispose();            ...        }

Problem is my code gets stuck at "GetResponseStream()".

 

Response headers:

Response: HTTP/1.1 200 OK
Connection: close
Cache-Control: no-cache
 
Response body:
temperature
60.5000
 
When I look at the content length in debug it has a value of -1 although I actually get a response body.
My guess is that there is something missing in the headers that makes my code break.

 

Any ideas how to make this work?

 

BR Larre



#2 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 01 January 2014 - 09:51 PM

Does it just hang when it hits the GetResponseStream() line or throw an exception?  Have you tried adjusting the Timeout property on the WebRequest to see if there is an issue with it timing out?  Also, how big of a response are you expecting?  If it's really big, could it be flooding the device's memory?


Check out my Netduino projects on GitHub.


#3 Larre

Larre

    Member

  • Members
  • PipPip
  • 11 posts

Posted 02 January 2014 - 09:29 AM

Hi,

No exception, it just hangs when it hits the GetResponseStream(). I haven't tried adjusting the Timeout since I seem to get a response straight away.

When I debug the response I can see that Content Length is set to -1 and Content Type is an empty string.

The expected response is really small, se below:

"temperature
60.5000"


#4 Larre

Larre

    Member

  • Members
  • PipPip
  • 11 posts

Posted 04 January 2014 - 11:06 AM

Solved!

 

Still think that there was a problem with headers in the previous test but I solved it using another way (as always) see below.

public double GetTemperature(string address)        {            var request = (HttpWebRequest)WebRequest.Create(_url + address);            request.Timeout = 3000;            double returnTemperature = -99D;            try            {                using (var stream = request.GetResponse().GetResponseStream())                using (var reader = new StreamReader(stream))                {                    char[] tempChar = new char[5];                    reader.Read(tempChar, 0, 5);                    var temperature = new string(tempChar);                    ...                }            }        }

Instead of "ReadToEnd" I am using the ordinary reader and in this specific case I only need the first few chars of the response.






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.