防止应用程序在应用程序内部抛出 Web 请求超时错误并捕获它



我正在努力处理 httpwebrequest 中的超时错误等,但它在调试中不断抛出错误,这意味着我无法在我的应用程序中显示状态码。我尝试使用尝试捕获,但是当我尝试获取响应后状态代码时。我唯一能从 catch 中得到的是我在调试中看到的错误描述,而不是响应的状态代码。

更新:(我最终这样做了。我仍然无法获得不同的状态代码,但我想这会起作用(

Try
                postresponse = DirectCast(postreq.GetResponse, HttpWebResponse)
                Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
                data = postreqreader.ReadToEnd
                'Get title
                Dim reg = New Regex("<title>(.*?)</title>")
                Dim matches = reg.Matches(data)
                Dim wtitle As String = "No title"
                For Each mat As Match In matches
                    wtitle = mat.Value.Replace("<title>", "").Replace("</title>", "")
                Next mat
                ListView1.Items(ci).SubItems(3).Text = wtitle
                'OK
                ListView1.Items(ci).SubItems(1).Text = "OK"
            Catch ex As Exception When WebExceptionStatus.Timeout
                ListView1.Items(ci).SubItems(1).Text = "Timeout"
            Catch ox As Exception When WebExceptionStatus.NameResolutionFailure
                MsgBox("Name resolution failure")
            End Try

源语言:

Dim data As String = Nothing
            Try
                Dim postreq As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
                postreq.Method = "GET"
                postreq.KeepAlive = True
                postreq.Timeout = 5000 '10s
                postreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729"
                postreq.ContentType = "application/x-www-form-urlencoded"
                postreq.Referer = "https://www.facebook.com"
                Dim postresponse As HttpWebResponse
                postresponse = DirectCast(postreq.GetResponse, HttpWebResponse)
                If postresponse.StatusCode = HttpStatusCode.OK Then
                    Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
                    data = postreqreader.ReadToEnd
                    'Get title
                    Dim reg = New Regex("<title>(.*?)</title>")
                    Dim matches = reg.Matches(data)
                    Dim wtitle As String = "No title"
                    For Each mat As Match In matches
                        wtitle = mat.Value.Replace("<title>", "").Replace("</title>", "")
                    Next mat
                    ListView1.Items(ci).SubItems(3).Text = wtitle
                    'OK
                    ListView1.Items(ci).SubItems(1).Text = "OK"
                Else
                    ListView1.Items(ci).SubItems(1).Text = "Fejl"
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

ex异常对象是泛型Exception类型,不提供任何特定于 HttpWebRequest 的信息。为WebException对象添加捕获块:

Try
    ' ..........
Catch ex As WebException
    If ex.Status = WebExceptionStatus.Timeout Then
        MsgBox("The request has timed out!")
    Else
        MsgBox(ex.Status)
    End If
Catch ex As Exception
    MsgBox(ex.ToString)
End Try

相关内容

  • 没有找到相关文章

最新更新