vb.net 在尝试从某个页面读取 xml 时处理服务器响应 [英] vb.net Handling the server response when trying to read xml from some page

查看:22
本文介绍了vb.net 在尝试从某个页面读取 xml 时处理服务器响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下函数从我的网站获取 html 源

I have the following function which i use to get html source from my website

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 等,我该如何独立处理这些错误?

some times the objResponse gives error "503 Server unavailable" and many other errors like 403 and so on, how can i handle each of those errors independently?

我怎样才能让这个函数在一段时间后重试请求?问题是 try 语句似乎没有处理这个问题,我不知道为什么我没有看到异常 MsgBox 但它在调试器上显示了错误.

how can i make this function retry the request after sometime? problem is the try statement doesn't seem to handle this and i am not sure why i don't see the exception MsgBox but it shows the error on the debugger.

推荐答案

将响应转换为 HttpWebResponse 对象并对其 StatusCode 属性执行 Select Case.你必须清理并完成它,但这里有一个例子:

Cast the response as a HttpWebResponse object and do a Select Case of it's StatusCode Property. You'll have to clean and finish this up, but here is an example:

    Select Case CType(objResponse, Net.HttpWebResponse).StatusCode

        Case Net.HttpStatusCode.InternalServerError
            'This is sloppy, but a quick example for one of your sub-questions.
            System.Threading.Thread.Sleep(10000)
            'Try again.
            objResponse = objRequest.GetResponse
        Case Net.HttpStatusCode.BadRequest
            'Error Handling
        Case Net.HttpStatusCode.OK
            'Proceed as normal.
        Case Else
            'Error Handling

    End Select

这篇关于vb.net 在尝试从某个页面读取 xml 时处理服务器响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆