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.Crea te(Url)
URLRes = URLReq.GetResponse()
sStream = URLRes.GetResponseStream()
Dim reader As String = New System.IO.StreamReader(sSt ream).Read ToEnd()
Return True
Catch ex As Exception
'Url not valid
Return False
End Try
End Function
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.Crea
URLRes = URLReq.GetResponse()
sStream = URLRes.GetResponseStream()
Dim reader As String = New System.IO.StreamReader(sSt
Return True
Catch ex As Exception
'Url not valid
Return False
End Try
End Function
You might have issues if you declare the timeout before the instance declaration:
URLReq = System.Net.WebRequest.Crea te(Url)
URLReq.Timeout = 5000
URLReq = System.Net.WebRequest.Crea
URLReq.Timeout = 5000
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.Crea te(Url)
URLRes = URLReq.GetResponse()
sStream = URLRes.GetResponseStream()
Dim reader As String = New System.IO.StreamReader(sSt ream).Read ToEnd()
URLReq.Timeout = 10
Return True
Catch ex As Exception
'Url not valid
Return False
End Try
End Function
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.Crea
URLRes = URLReq.GetResponse()
sStream = URLRes.GetResponseStream()
Dim reader As String = New System.IO.StreamReader(sSt
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.Crea te(Url)
URLReq.Timeout = 5000
URLRes = URLReq.GetResponse()
sStream = URLRes.GetResponseStream()
Dim reader As String = New System.IO.StreamReader(sSt ream).Read ToEnd()
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.
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.Crea
URLReq.Timeout = 5000
URLRes = URLReq.GetResponse()
sStream = URLRes.GetResponseStream()
Dim reader As String = New System.IO.StreamReader(sSt
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.
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.
ASKER
Here it is
If Collection1.EkItems.Length Then
Collection1.Text = Collection1.EkItems(0).Htm l
End If
If CheckValidUrl(RssAggregato r4.URL) = True Then
RssAggregator4.URL = RssAggregator4.URL
Else
RssAggregator4.Visible = False
Label1.Visible = True
End If
If CheckValidUrl(RssAggregato r2.url) = True Then
RssAggregator2.URL = RssAggregator2.url
Else
RssAggregator2.Visible = False
Label3.visible = True
End If
If CheckValidUrl(rssaggregato r3.url) = True Then
RssAggregator3.URL = rssaggregator3.url
Else
RssAggregator3.Visible = False
Label2.visible = True
End If
If Collection1.EkItems.Length
Collection1.Text = Collection1.EkItems(0).Htm
End If
If CheckValidUrl(RssAggregato
RssAggregator4.URL = RssAggregator4.URL
Else
RssAggregator4.Visible = False
Label1.Visible = True
End If
If CheckValidUrl(RssAggregato
RssAggregator2.URL = RssAggregator2.url
Else
RssAggregator2.Visible = False
Label3.visible = True
End If
If CheckValidUrl(rssaggregato
RssAggregator3.URL = rssaggregator3.url
Else
RssAggregator3.Visible = False
Label2.visible = True
End If
As I understand you, if CheckValidUrl(RssAggregato r4.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.Crea te(Url)
URLReq.Timeout = 5000
URLRes = URLReq.GetResponse()
sStream = URLRes.GetResponseStream()
Dim reader As String = New System.IO.StreamReader(sSt ream).Read ToEnd()
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.
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=
Console.WriteLine("check2=
Console.WriteLine("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.Crea
URLReq.Timeout = 5000
URLRes = URLReq.GetResponse()
sStream = URLRes.GetResponseStream()
Dim reader As String = New System.IO.StreamReader(sSt
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.
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
HOw do I proceed
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you
Thanks for the question and the points.
Dim WebRequest As HttpWebRequest
Dim value As Integer
WebRequest.Timeout = value