Include timeout for a http request

krbnldy
krbnldy used Ask the Experts™
on
I have the following code and would like to include code that says if it takes more than 5 second to get the results page then return false.  How do i modify the code to make that happen

Protected Function CheckValidUrl(ByVal Url As String) As Boolean
        Dim sStream As IO.Stream
        Dim URLReq As Net.HttpWebRequest
        Dim URLRes As Net.HttpWebResponse
        Try
            URLReq = System.Net.WebRequest.Create(Url)
            URLRes = URLReq.GetResponse()
            sStream = URLRes.GetResponseStream()
            Dim reader As String = New System.IO.StreamReader(sStream).ReadToEnd()
            Return True
        Catch ex As Exception
            'Url not valid
            Return False
        End Try
    End Function

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
If you use System.Net.HttpWebRequest you can use the timeout property. Sets the timeout of the  GetResponse and GetRequestStream methods in milliseconds. Use in conjunction with HttpWebResponse.

Dim WebRequest As HttpWebRequest
Dim value As Integer

WebRequest.Timeout = value
You might have issues if you declare the timeout before the instance declaration:
            URLReq = System.Net.WebRequest.Create(Url)
            URLReq.Timeout = 5000

Author

Commented:
Would the following be the way to modify the code?

Protected Function CheckValidUrl(ByVal Url As String) As Boolean
        Dim sStream As IO.Stream
        Dim URLReq As Net.HttpWebRequest
        Dim URLRes As Net.HttpWebResponse
        Try
            URLReq = System.Net.WebRequest.Create(Url)
            URLRes = URLReq.GetResponse()
            sStream = URLRes.GetResponseStream()
            Dim reader As String = New System.IO.StreamReader(sStream).ReadToEnd()
            URLReq.Timeout = 10
            Return True
        Catch ex As Exception
            'Url not valid
            Return False
        End Try
    End Function
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Here's what I had:

Function CheckValidUrl(ByVal Url As String) As Boolean
        Dim sStream As IO.Stream
        Dim URLReq As Net.HttpWebRequest
        Dim URLRes As Net.HttpWebResponse
        Try
            URLReq = System.Net.WebRequest.Create(Url)
            URLReq.Timeout = 5000
            URLRes = URLReq.GetResponse()
            sStream = URLRes.GetResponseStream()
            Dim reader As String = New System.IO.StreamReader(sStream).ReadToEnd()
            Return True
        Catch ex As Exception
            'Url not valid
            Return False
        End Try
End Function

Note that the timeout is in milliseconds so 10ms might be a bit too small to work well.

Author

Commented:
i have 3 different RSS feeds that i am using this function to check for validity and if one times out it does not display any of the other links when I place the timeout at that position.
Please post the section of your code where you call the function.

Author

Commented:
Here it is
If Collection1.EkItems.Length Then
            Collection1.Text = Collection1.EkItems(0).Html
        End If
        If CheckValidUrl(RssAggregator4.URL) = True Then
            RssAggregator4.URL = RssAggregator4.URL
        Else
            RssAggregator4.Visible = False
            Label1.Visible = True
        End If

        If CheckValidUrl(RssAggregator2.url) = True Then
            RssAggregator2.URL = RssAggregator2.url
        Else
            RssAggregator2.Visible = False
            Label3.visible = True
        End If

        If CheckValidUrl(rssaggregator3.url) = True Then
            RssAggregator3.URL = rssaggregator3.url
        Else
            RssAggregator3.Visible = False
            Label2.visible = True
        End If
As I understand you, if CheckValidUrl(RssAggregator4.URL) = False then RssAggregator2.url and rssaggregator3.url are not checked regardless of their validity?

To reproduce the if-then nested loops I used this test code:
Module Module1
    Sub Main()
        Dim check As Integer
        Dim check1 As String
        Dim check2 As String
        Dim check3 As String
        If check = 0 Then
            If CheckValidUrl("http://invalid/") = True Then
                check1 = "valid"
            Else
                check1 = "invalid"
            End If
            If CheckValidUrl("http://invalid2/") = True Then
                check2 = "valid"
            Else
                check2 = "invalid"
            End If
            If CheckValidUrl("http://www.msn.com/") = True Then
                check3 = "valid"
            Else
                check3 = "invalid"
            End If
        End If
        Console.WriteLine("check1=" & check1)
        Console.WriteLine("check2=" & check2)
        Console.WriteLine("check3=" & check3)
    End Sub
    Function CheckValidUrl(ByVal Url As String) As Boolean
        Dim sStream As IO.Stream
        Dim URLReq As Net.HttpWebRequest
        Dim URLRes As Net.HttpWebResponse
        Try
            URLReq = System.Net.WebRequest.Create(Url)
            URLReq.Timeout = 5000
            URLRes = URLReq.GetResponse()
            sStream = URLRes.GetResponseStream()
            Dim reader As String = New System.IO.StreamReader(sStream).ReadToEnd()
            Return True
        Catch ex As Exception
            'Url not valid
            Return False
        End Try
    End Function
End Module

I get the expected results and all three checks are completed.

Author

Commented:
I have never used a module before so the Module  is showing "Module statements can only occur at file or namespace level"

HOw do I proceed
That code is cut-and-paste ready if you create a console application in vb.net then in the code of the newly-created app replace:
Module Module1

    Sub Main()

    End Sub

End Module

With the code in my last post.

Author

Commented:
thank you
Thanks for the question and the points.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial