我有以下功能,用于从我的网站获取 html 源代码
Public Function GetPageHTML(ByVal URL As String, _
Optional ByVal TimeoutSeconds As Integer = 10) _
As String
' Retrieves the HTML from the specified URL,
' using a default timeout of 10 seconds
Dim objRequest As Net.WebRequest
Dim objResponse As Net.WebResponse
Dim objStreamReceive As System.IO.Stream
Dim objEncoding As System.Text.Encoding
Dim objStreamRead As System.IO.StreamReader
Try
' Setup our Web request
objRequest = Net.WebRequest.Create(URL)
objRequest.Timeout = TimeoutSeconds * 1000
' Retrieve data from request
Try
objResponse = objRequest.GetResponse 'some times it gives an error server unavailable 503
Catch ex As WebException
MsgBox(ex.Message)
End Try
objStreamReceive = objResponse.GetResponseStream
objEncoding = System.Text.Encoding.GetEncoding( _
"utf-8")
objStreamRead = New System.IO.StreamReader( _
objStreamReceive, objEncoding)
' Set function return value
GetPageHTML = objStreamRead.ReadToEnd()
' Check if available, then close response
If Not objResponse Is Nothing Then
objResponse.Close()
End If
Catch
' Error occured grabbing data, simply return nothing
Return ""
End Try
End Function
有时 objResponse 给出错误“503 服务器不可用”和许多其他错误,如 403 等,我如何独立处理这些错误中的每一个?
我怎样才能让这个函数在一段时间后重试请求?问题是 try 语句似乎没有处理这个问题,我不确定为什么我没有看到异常 MsgBox 但它在调试器上显示错误。