System.IO.HttpWebResponse freezes

Hi guys,

I’m editing a gadgeteer project designed for a FEZ Cobra 2 Net (I’m using framework 4.2, not the last 4.3!).
I wrote few lines of code for read the source code of an HTML page. This is the code:


Dim req As System.Net.HttpWebRequest
Dim targetURI As Uri
targetURI = New Uri("http://192.168.2.60/info")
req = DirectCast(HttpWebRequest.Create(targetURI), System.Net.HttpWebRequest)
With req
    .Timeout = 5000
    .Method = "GET"
    .ContentLength = 0
    .KeepAlive = False
End With
Dim strHtmlRead As String
Using resp As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)
    Using respStream As New System.IO.StreamReader(resp.GetResponseStream())
        strHtmlRead = respStream.ReadToEnd()
        respStream.Close()
    End Using
End Using
req = Nothing

If the remote host 192.168.2.60 is online this code works properly.
If the remote host 192.168.2.60 is offline (turned off, ethernet cable unplugged, etc.) this code won’t work: debugging I found that calling req.GetResponse() freezes the execution if the remote host is not reachable.
I found some topic after searching on google in which someone said about adding the ContentLength to the request object but nothing changes (I set to zero the ContentLength, is it wrong?) .

I liked to avoid the HTML source request if the host 192.168.2.60 wasn’t online trying a PING, but I didn’t find a PING function.

What can I do to fix this issue?
Thanks
Big

@ andre.m - Thanks for suggestion. Can you provide me an example (or a link)?

@ andre.m - I develop also in C# and I’ve basics in C++, it’s the same to me. :smiley:
Can you provide them anyway?

1 Like

maybe you can use the Property “IsReceived” before you call the “getResponse”.
So first you wait for the response received and then after a while you abort

@ VB-Daniel - IsReceived is a property of HttpRequest class (Gadgeteer.Networking namespace), I’m using HttpWebRequest class (System.Net namespace).
I don’t know if one is based on the other one or if they are 2 different classes “standalone”.

Do you suggest to use HttpRequest instead of HttpWebRequest?

I never use the System.Net namespace before … so down’t know

just try and error ???
sometimes the easiest way

1 Like

Yeah, it works using the namespace Gadgeteer.Networking!!
Thanks a lot!