从webbrowser阻止网站

时间:2017-10-21 06:17:09

标签: webbrowser-control

是否可以在网站实际加载之前阻止Visual Basic中的网站。

例如:

如果我尝试加载www.google.co.uk并点击它,则不会加载网站但会显示错误消息。我试过搜索但找不到我要找的东西。

2 个答案:

答案 0 :(得分:0)

 '
  '
Dim MyURL As String
  '
  '  
Private Sub Command1_Click()
 MyURL = "http://www.website.com"
 WebBrowser1.Navigate2 MyURL
End Sub

Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
 If Instr(URL, MyURL) > 0 Then
   Cancel = True ' Do not allow
 End If
End Sub

答案 1 :(得分:0)

这是你要问的一件非常容易的事。看起来像这样。您基本上可以在导航和任何加载之前进入导航事件。然后,您可以检查所需的URL,然后取消导航或重新路由到已批准的网站。

Public Class Form1
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
    End Sub
    Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
        If e.Url.ToString.Contains("www.badwebsite.com") Then
            e.Cancel = True
            'WebBrowser1.Navigate("http://www.goodwebsite.com")
        End If
    End Sub
End Class