Link to home
Start Free TrialLog in
Avatar of krbnldy
krbnldy

asked on

Include timeout for a http request

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

Avatar of theplonk
theplonk
Flag of Australia image

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
Avatar of ddrudik
You might have issues if you declare the timeout before the instance declaration:
            URLReq = System.Net.WebRequest.Create(Url)
            URLReq.Timeout = 5000
Avatar of krbnldy
krbnldy

ASKER

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
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.
Avatar of krbnldy

ASKER

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.
Avatar of krbnldy

ASKER

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.
Avatar of krbnldy

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of krbnldy

ASKER

thank you
Thanks for the question and the points.