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

WebRequest Timeout property doesn't work

WebRequest

  • Please log in to reply
6 replies to this topic

#1 John West

John West

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 17 February 2014 - 07:58 PM

In my understanding, the GetResponse call below should throw an exception after two seconds if the server it's trying to connect to is offline, or there are any other connectivity issues.  Unfortunately, this is not happening. If I have the server offline that it's trying to connect to, it just hangs on the GetResponse call.  Does Timeout not work on .netmf?

 

  var req = WebRequest.Create("http://xxxxxxxx/api/...quekey=12345");   req.ContentType = "application/json";   req.Timeout = 1000 * 2;

  using (var response = req.GetResponse()) //hangs here   {

...



#2 GaryDyksman

GaryDyksman

    New Member

  • Members
  • Pip
  • 6 posts

Posted 18 February 2014 - 06:23 AM

public static string GetRequest(Request requesttype)        {            try            {                var webrequest = HttpWebRequest.Create("http://api.worldweatheronline.com/free/v1/tz.ashx?key=xxxxxxxxxxxxxxxxxxxxxxxxxx&q=pretoria&format=json");                webrequest.Method = "GET";                webrequest.Timeout = 15000;                var response = webrequest.GetResponse();                using (var reader = new StreamReader(response.GetResponseStream()))                {                    var buffer = new char[5];                    var builder = new StringBuilder();                    while (reader.Read(buffer, 0, 5) > 0)                        builder.Append(buffer);                    return builder.ToString();                }            }            //catch (WebException wex)            //{            //    var reader = new StreamReader(wex.Response.GetResponseStream());            //    var response = reader.ReadToEnd();            //    return response;            //}            catch (Exception ex)            {                return ex.Message;            }        }

This is what my WebRequest looks like and works perfectly. Requests over SSL hangs the GetResponse(), and I always use Fiddler to check that my request is correct.



#3 jur

jur

    New Member

  • Members
  • Pip
  • 4 posts

Posted 22 September 2014 - 09:48 PM

I have been bothering with this same issue for couple of hours with no result. I want to log to my local PHP webservice (which is deliberately offline for testing purposes) unfortunately Netduino does not throw timeout exception, but it hangs on this line even though timeout is set to 5000 ms.

HttpWebResponse response = (HttpWebResponse) webrequest.GetResponse();

I include my piece of code:

 try
            {
                if (_isNetworkAvailable)
                {
                    HttpWebRequest webrequest = (HttpWebRequest) HttpWebRequest.Create("http://" + server + url);
                    webrequest.Method = "GET";
                    webrequest.Timeout = 5000;
                    webrequest.KeepAlive = false;
                        
                    HttpWebResponse response = (HttpWebResponse) webrequest.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        using (var reader = new StreamReader(response.GetResponseStream()))
                        {
                            var buffer = new char[5];
                            var builder = new StringBuilder();
                            while (reader.Read(buffer, 0, 5) > 0)
                                builder.Append(buffer);

                            reader.Close();
                            reader.Dispose();
                            response.Close();
                            response.Dispose();
                  
                            return builder.ToString();
                        }
                    }
                    else { return null; }

                }
                else {
                    throw new Exception("Network cable unavailable");
                }
            }
            catch (WebException e)
            {                
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    
                }
                return null;
            }
            catch(Exception ex) {
                Debug.Print(ex.Message);
                return null; 
            }


#4 whitecap

whitecap

    Member

  • Members
  • PipPip
  • 11 posts

Posted 08 October 2014 - 12:30 AM

This is a late reply I know but...

 

The webrequest timeout appears to be broken in the Netduino. If the webrequest target device is not available the request will simply wait forever and hang.

 

I got around the problem by wrapping the webrequest in a ping request. If the ping succeeds the webrequest is executed. If the ping fails the request is bypassed.

 

I used this version of Ping and it appears to work well:

 

https://github.com/gittela/pingbox



#5 jur

jur

    New Member

  • Members
  • Pip
  • 4 posts

Posted 08 October 2014 - 06:16 AM

Thanks for reply, I have done the same workaround -> since could not find better one, but unfortunately If remote host replies to ping does not mean, that remote webserver is running :( 



#6 Larre

Larre

    Member

  • Members
  • PipPip
  • 11 posts

Posted 08 October 2014 - 07:29 AM

I had the same issue but I solved it in another way. I did the webrequest in a separate thread  and used thread.join(myTimeout) to make it stop if it hanged.



#7 Spiked

Spiked

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 09 October 2014 - 04:18 AM

I have heard it is a known bug in MF. So maybe fixed next MF release. It is probably related to why they are redoing the TCP stack, among other things.







Also tagged with one or more of these keywords: WebRequest

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.