VB.NET 函数作为字符串,返回 False 会是布尔值吗?

发布于 2024-12-05 12:35:38 字数 1467 浏览 1 评论 0原文

我有一个 HTTP 类,它从 URL 获取内容、将 POST 内容获取到 URL 等,然后返回原始 HTML 内容。

在类内部的函数中,它检测是否存在 HTTP 错误,如果是,我想返回 false,但是如果我声明函数返回 String,这会起作用吗?

我正在尝试执行的操作的代码示例(请注意返回内容,如果检测到 HTTP 错误代码则返回 False)

Public Function Get_URL(ByVal URL As String) As String
    Dim Content As String = Nothing
    Try
        Dim request As Net.HttpWebRequest = Net.WebRequest.Create(URL)
        ' Request Settings
        request.Method = "GET"
        request.KeepAlive = True
        request.AllowAutoRedirect = True
        request.Timeout = MaxTimeout
        request.CookieContainer = cookies
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.60 Safari/534.24"
        request.Timeout = 60000
        request.AllowAutoRedirect = True
        Dim response As Net.HttpWebResponse = request.GetResponse()
        If response.StatusCode = Net.HttpStatusCode.OK Then
            Dim responseStream As IO.StreamReader = New IO.StreamReader(response.GetResponseStream())
            Content = responseStream.ReadToEnd()
        End If
        response.Close()
    Catch e As Exception
        HTTPError = e.Message
        Return False
    End Try
    Return Content
End Function

和用法示例:

Dim Content As String = Get_URL("http://www.google.com/")
If Content = False Then
    MessageBox.Show("A HTTP Error Occured: " & MyBase.HTTPError)
    Exit Sub
End If

I have a HTTP class that gets content from URL's, POST's content to URL's etc and then returns the raw HTML content.

In the function inside of the class it detects if there is a HTTP error and if so I would like to return false but will this work if I have declared the function to return a String?

Code Sample of what I am trying to do (Note the Return Content & Return False if a HTTP error code is detected)

Public Function Get_URL(ByVal URL As String) As String
    Dim Content As String = Nothing
    Try
        Dim request As Net.HttpWebRequest = Net.WebRequest.Create(URL)
        ' Request Settings
        request.Method = "GET"
        request.KeepAlive = True
        request.AllowAutoRedirect = True
        request.Timeout = MaxTimeout
        request.CookieContainer = cookies
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.60 Safari/534.24"
        request.Timeout = 60000
        request.AllowAutoRedirect = True
        Dim response As Net.HttpWebResponse = request.GetResponse()
        If response.StatusCode = Net.HttpStatusCode.OK Then
            Dim responseStream As IO.StreamReader = New IO.StreamReader(response.GetResponseStream())
            Content = responseStream.ReadToEnd()
        End If
        response.Close()
    Catch e As Exception
        HTTPError = e.Message
        Return False
    End Try
    Return Content
End Function

And usage example:

Dim Content As String = Get_URL("http://www.google.com/")
If Content = False Then
    MessageBox.Show("A HTTP Error Occured: " & MyBase.HTTPError)
    Exit Sub
End If

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小沫ゞ 2024-12-12 12:35:38

通常在这种类型的场景中,您会抛出一个具有更详细信息的新异常,并让异常冒泡到主代码处理(或者只是让原始异常冒泡而不首先捕获它)。

Catch e As Exception
    ' wrap the exception with more info as a nested exception
    Throw New Exception("Error occurred while reading '" + URL + "': " + e.Message, e)
End Try

内部使用示例:

Dim content As String = ""
Try
    content = Get_URL("http://www.google.com/")
Catch e As Exception
    MessageBox.Show(e.Message)
    Exit Sub
End Try

Usually in this type of scenario, you would throw a new exception with more detailed information, and let the exception bubble up to the processed by the main code (or just let the original exception bubble up without Catching it in the first place).

Catch e As Exception
    ' wrap the exception with more info as a nested exception
    Throw New Exception("Error occurred while reading '" + URL + "': " + e.Message, e)
End Try

Inside the usage example:

Dim content As String = ""
Try
    content = Get_URL("http://www.google.com/")
Catch e As Exception
    MessageBox.Show(e.Message)
    Exit Sub
End Try
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文