我正在使用Visual Studio 2015 Community Edition和Visual Basic制作一个简单的Web浏览器。我在下面添加了代码。我的浏览器工作正常,但我的问题是它似乎只能使用HTTPS协议链接。我认为这是因为http会自动抛出一个错误,导致我的错误处理程序运行,它会使用https前缀重试网站。但是我不知道那个错误是什么或如何解决它...你能帮忙吗?
CODE:
Public Class Form1
'One button for go. Other for settings -incognito, history, homepage????
'Window button actions
Private Sub Green_Click(sender As Object, e As EventArgs) Handles Green.Click
Me.WindowState = FormWindowState.Maximized
End Sub
Private Sub Yellow_Click(sender As Object, e As EventArgs) Handles Yellow.Click
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub Red_Click(sender As Object, e As EventArgs) Handles Red.Click
Me.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
TitleText.Parent = Grey
TitleText.BackColor = Color.Transparent
TitleTextDivider.Parent = Grey
TitleTextDivider.BackColor = Color.Transparent
VersionText.Parent = Grey
VersionText.BackColor = Color.Transparent
End Sub
Public Sub GoButton_Click(sender As Object, e As EventArgs) Handles GoButton.Click
'Add checking blacklist and adding link to history file here later. Use separate subs and call them here for better organisation.
Dim Input As String = TextBox1.Text
'Prefix with http and www
Input = "http://www." + Input
'Convert to Uri
Dim Url As Uri = New Uri(Input)
'Set as url
WebBrowser1.Url = Url
'Refresh
WebBrowser1.Refresh()
'On error, retry with https
On Error GoTo HTTPS_Handler
HTTPS_Handler:
'New String URL, replacing http with https
Dim HTTPS_Input As String = Replace(Input, "http", "https")
'All we did last time, again...
Dim NewUrl As Uri = New Uri(HTTPS_Input)
WebBrowser1.Url = NewUrl
WebBrowser1.Refresh()
End Sub
End Class
不要担心第一位,只关注“Public sub GoButton_clicked”之后的位......
答案 0 :(得分:0)
您尝试使用旧的VB6样式的错误处理。正如上面的评论所表明的那样,在VB.Net中使用更新的Try \ Catch语法会更好。
如果你想让它像这样写...
Public Sub GoButton_Click(sender As Object, e As EventArgs) Handles GoButton.Click
On Error GoTo HTTPS_Handler
Dim Input As String = TextBox1.Text
Input = "http://www." + Input
Dim Url As Uri = New Uri(Input)
WebBrowser1.Url = Url
WebBrowser1.Refresh()
Exit Sub
HTTPS_Handle:
Dim HTTPS_Input As String = Replace(Input, "http", "https")
Dim NewUrl As Uri = New Uri(HTTPS_Input)
WebBrowser1.Url = NewUrl
WebBrowser1.Refresh()
End Sub
如果没有错误,请注意“退出Sub”以防止它进入错误处理代码。它目前正在做什么,所以你可能没有任何错误,只是无意中刷新了页面。
另外,您可以在错误处理中查看错误变量,请参阅Err.Description和Err.Number