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?