【问题标题】:Prevent application from throwing webrequest timeout error inside application and catch it防止应用程序在应用程序内抛出 webrequest 超时错误并捕获它
【发布时间】:2017-04-14 09:41:08
【问题描述】:

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

【问题讨论】:

    标签: vb.net timeout httpwebrequest


    【解决方案1】:

    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
    

    【讨论】:

    • 是的,我实际上最终做了类似的事情。我已经更新了我的问题。不过谢谢。虽然我仍然无法获得状态码,但我想这可以。
    猜你喜欢
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 2014-12-05
    • 2018-11-03
    • 2019-01-01
    相关资源
    最近更新 更多