VB.NET 函数作为字符串,返回 False 会是布尔值吗?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常在这种类型的场景中,您会抛出一个具有更详细信息的新异常,并让异常冒泡到主代码处理(或者只是让原始异常冒泡而不首先捕获它)。
内部使用示例:
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).
Inside the usage example: