我有一个vb.net webbrowser控件,可以访问网站并自动登录。即使有人在表单中输入了错误的密码,它也能正常工作。但是一旦在错误的密码后输入了正确的密码,登录表单会再次弹出,他们必须再次输入用户/密码,它将在第二次运行。我已经包含了webbrowser表单的完整代码。发生上述同样问题时发生的另一个奇怪的事情是,它会弹出一个默认浏览器到列出的网址,但最后会在http://url%20-%20#b8716a8显示 有什么想法吗?
Imports System.IO
Imports Microsoft.Win32
Public Class frm_webbrowser
Private cntAttempts1 = 0
Private Const BrowserKeyPath As String = "\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION"
Private Sub frm_webbrowser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateBrowserKey()
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Navigate(“url", "_top", Nothing, "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36")
End If
End Sub
Private Sub frm_webbrowser_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
RemoveBrowerKey()
End Sub
Private Sub CreateBrowserKey(Optional ByVal IgnoreIDocDirective As Boolean = False)
Dim basekey As String = Microsoft.Win32.Registry.CurrentUser.ToString
Dim value As Int32
Dim thisAppsName As String = My.Application.Info.AssemblyName & ".exe"
Select Case (New WebBrowser).Version.Major
Case 8
If IgnoreIDocDirective Then
value = 8888
Else
value = 8000
End If
Case 9
If IgnoreIDocDirective Then
value = 9999
Else
value = 9000
End If
Case 10
If IgnoreIDocDirective Then
value = 10001
Else
value = 10000
End If
Case 11
If IgnoreIDocDirective Then
value = 11001
Else
value = 11000
End If
Case Else
Exit Sub
End Select
Microsoft.Win32.Registry.SetValue(Microsoft.Win32.Registry.CurrentUser.ToString & BrowserKeyPath,
Process.GetCurrentProcess.ProcessName & ".exe",
value,
Microsoft.Win32.RegistryValueKind.DWord)
End Sub
Private Sub RemoveBrowerKey()
Dim key As Microsoft.Win32.RegistryKey
key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(BrowserKeyPath.Substring(1), True)
key.DeleteValue(Process.GetCurrentProcess.ProcessName & ".exe", False)
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
WebBrowser1.Document.GetElementById("username").SetAttribute("value", user.Text)
WebBrowser1.Document.GetElementById("password").SetAttribute("value", pass.Text)
WebBrowser1.Document.All("submit").InvokeMember("click")
cntAttempts1 += 1
If cntAttempts1 = 3 Then
cntAttempts1 = 0
WebBrowser1.Stop()
MsgBox("Invalid username/password please re-enter.")
Dim box = New frm_weblogin()
box.Show()
Me.Close()
End If
End Sub
Private Sub WebBrowser1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
e.Cancel = True
WebBrowser1.Navigate(WebBrowser1.StatusText)
End Sub
答案 0 :(得分:0)
如果有其他人感兴趣,我可以通过在DocumentCompleted中添加以下代码来解决此问题。
If e.Url.AbsolutePath <> TryCast(sender, WebBrowser).Url.AbsolutePath Then
Return
End If
我在默认浏览器中出现的奇怪弹出是由MsgBox引起的,所以我只是将其删除并创建了一个表示用户名/密码无效且有登录字段的表单。