【问题标题】:HttpStatusCode reading problemsHttpStatusCode 读取问题
【发布时间】:2013-07-11 11:17:43
【问题描述】:

我在从响应中获取 HttpStatusCode 时遇到了小问题。问题是当文件存在时我得到响应并且可以读取读取状态,但是当文件不存在时我看不到任何状态,即使我要求显示状态字符串。这是我的代码:

 Dim urls As New List(Of String)
        urls.Add("http://www.domain.com/test.php")
        urls.Add("http://www.domain.com/test2.php")
        urls.Add("http://www.domain.com/index.php")


        For Each Url As String In urls
            Dim response As HttpWebResponse = Nothing
            Try
                Dim request As HttpWebRequest = Net.HttpWebRequest.Create(Url)
                request.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)"
                request.Method = "GET"
                response = request.GetResponse()

            Catch webex As WebException
            End Try

            If response.StatusCode = HttpStatusCode.OK = True Then
                MsgBox("File Url is correct: " & response.StatusCode.ToString)
            ElseIf response.StatusCode = HttpStatusCode.NotFound = True Then
                MsgBox("File Url is incorrect: " & Url)
            Else
                MsgBox(response.StatusCode.ToString)
            End If
        Next

【问题讨论】:

标签: .net vb.net


【解决方案1】:

当服务器没有返回成功状态码(2xx)时,框架总是抛出异常。但是,您仍然可以从异常对象中获得响应。

Function GetResponse(url As Uri) As WebResponse
    Dim response As WebResponse
    Dim request As HttpWebRequest = HttpWebRequest.Create(url)
    Try
        response = request.GetResponse()
    Catch serverErrors As WebException When serverErrors.Response IsNot Nothing
        response = serverErrors.Response
    Catch otherExceptions As Exception
        DoSomethingWith(otherExceptions)
    End Try
    Return response
End Function

【讨论】:

    【解决方案2】:

    问题在于,当文件不存在时,它会生成 WebException,而您的代码会默默地“吞下”这些异常。 IE。它抓住了它,什么也不做。

    您需要在 catch 语句中添加一些检查错误的代码。

    这可能是 How to properly catch a 404 error in .NET 的副本(尽管是 C# 而不是 VB)

    【讨论】:

      【解决方案3】:

      如果我错了,请纠正我,但不应该这部分:

      ElseIf response.StatusCode = HttpStatusCode.NotFound = True Then
                 MsgBox("File Url is incorrect: " & Url)
      

      其实是:

      ElseIf response.StatusCode = HttpStatusCode.NotFound = True Then
                 MsgBox("File Url is incorrect: " & Url & response.StatusCode.ToString)
      

      如果您希望显示StatusCode

      关于您的评论:

      但是当文件不存在时,我看不到任何状态

      代码正在访问NotFound 枚举并进入代码块,但您并没有准确地显示代码中的状态。

      【讨论】:

      • 我想看看字符串哪个文件没有t exist first when status is HttpStatusCode.NotFound. If it would give me another status, why it doesn't show me in Else MsgBox(response.StatusCode.ToString)` ?
      • " why it doesn't show me in Else MsgBox(response.StatusCode.ToString) " - 因为代码正在输入ElseIf。执行不会继续到Else。如果您希望对它们全部进行单独评估,那么您需要对所有这些都使用 If 语句。
      • 但如果我将ElseIf response.StatusCode = HttpStatusCode.NotFound = True Then MsgBox("File Url is incorrect: " & Url) 更改为ElseIf response.StatusCode = HttpStatusCode.OK = False Then MsgBox("File Url is incorrect: " & Url) 我也看不到它。
      • 我现在明白了。您需要阅读执行流程。如果你有一堆 else if,代码只会输入它遇到的第一个。然后它将跳出 if/elseif 块并继续。如果您希望每个都被评估,您需要将它们全部更改为 If 语句。
      • 好的,已修复,将 `Catch webex As WebException End Try` 替换为 ` Catch webex As WebException 'MsgBox(webex.Message.ToString) Continue For End Try` 它使函数继续运行。跨度>
      猜你喜欢
      • 1970-01-01
      • 2010-11-06
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多