防止应用程序在应用程序中抛出webrequest超时错误并捕获它

时间:2017-04-14 09:41:09

标签: vb.net timeout httpwebrequest

我正在处理httpwebrequest中的超时错误等问题,但它一直在调试中抛出错误,这意味着我无法在我的应用程序中显示状态代码。我试图使用Try catch,但是当我试图抓住postresponse状态代码。我从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

1 个答案:

答案 0 :(得分:3)

ex异常对象属于通用Exception类型,它不提供任何HttpWebRequest特定信息。为WebException对象添加catch块:

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