2016-08-01 54 views
0

我正在使用Visual Studio 2015社区版和Visual Basic制作一个简单的网络浏览器。我已经添加了下面的代码。我的浏览器工作,但我的问题是,它似乎只能用于HTTPS协议链接。我认为这是因为http会自动抛出一个错误,这会导致我的错误处理程序运行,这会使用https前缀重试网站。但是我不知道那个错误可能是什么或者如何解决它......你能帮忙吗?我的网页浏览器只能使用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

不要担心第一位,只专注于“公共子GoButton_clicked”后的位......

+0

应该工作,但可能不处理的事实,一些网站尝试自动重定向到HTTPS(谷歌是一个这个例子)。阅读正确的错误处理使用Try/Catch,并告诉我们你得到什么错误。 http://stackoverflow.com/documentation/vb.net/4232/error-handling/14828/try-catch-finally-statement#t=201608011838059034895 – TyCobb

+0

有没有办法做到这一点,而不做尝试/抓住?我可以以某种方式测试一个网站是否试图重定向我? – UltraLuminous

+0

这不是我说的。使用try/catch,以便捕获实际抛出的错误,然后搜索并尝试解决问题。现在,它正在炸毁,你正抛出异常,但不知道例外是什么。 – TyCobb

回答

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 

注意“退出小组”,以防止它进入了错误处理代码,如果没有错误。它会按照目前的状况进行操作,因此您可能根本没有任何错误,只是无意中无意间刷新了页面。

加上有错误的变量,你可以在错误处理查见Err.Description和Err.Number的

相关问题