Webbrowser控件

怎么编程把用户名,密码提交到网页上的登录页?


'首先在程序中加入Webbrowser控件并加入引用 Microsoft HTML Object Library。
'假设你的HTML页面表单代码如下:
'<form method="POST" action="http://chen/dll/chat/chatmain.exe/RegUser">
'  <p>请填写下面表单注册(*项为必添项)</p>
'  <p>*姓名<input type="text" name="Name" size="20"></p>
'  <p>*昵称<input type="text" name="NickName" size="20"></p>
'  <p>电子邮件<input type="text" name="EMail" size="20"></p>
'  <p>*密码<input type="text" name="Password" size="20"></p>
'  <p><input type="submit" value="提交" name="B1"><input type="reset" value="全部重写" name="B2"></p>
'</form>
'注意其中元素的type?Name?value属性?然后VB中的代码如下:
Private Sub Command1_Click()
    WebBrowser1.Navigate "http://chen/chat/newuser.htm"
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    Dim vDoc, vTag
    Dim i As Integer
     
    Set vDoc = WebBrowser1.Document
    List1.Clear
    For i = 0 To vDoc.All.length - 1
        If UCase(vDoc.All(i).tagName) = "INPUT" Then
            Set vTag = vDoc.All(i)
            If vTag.Type = "text" Or vTag.Type = "password" Then
                List1.AddItem vTag.Name
                Select Case vTag.Name
                    Case "Name"
                        vTag.Value = "IMGod"
                    Case "NickName"
                        vTag.Value = "IMGod"
                    Case "Password"
                        vTag.Value = "IMGodpass"
                    Case "EMail"
                        vTag.Value = "IMGod@paradise.com"
                End Select
            ElseIf vTag.Type = "submit" Then
                vTag.Click
            End If
        End If
    Next i
End Sub
'点击Command1就可以自动填表并提交了

另个代码:
'要引用一个Microsoft XML 的组件

Option Explicit
Dim xml As New XMLHTTP
Private Sub Command1_Click()
   Call AccessNet
End Sub

Private Sub AccessNet()
    On Error Resume Next
    Dim str1 As String
    xml.Open "POST", "http://yourWeb/handle.asp", False
   
    xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
   
    xml.send "UserName=" & Text1.Text & "&Password=" & Text2.Text
   
    If xml.Status = 200 Then
       str1 = StrConv(xml.responseBody, vbUnicode) '返回的内容
       MsgBox str1
    End If
End Sub


'或者改为:

Option Explicit
Dim xml As Object
Private Sub Command1_Click()
   Call AccessNet
End Sub

Private Sub AccessNet()
    On Error Resume Next
    Dim str1 As String
    Set xml = CreateObject("MSXML2.XMLHTTP")
    xml.Open "POST", "http://yourWeb/handle.asp", False
   
    xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
   
    xml.send "UserName=" & Text1.Text & "&Password=" & Text2.Text
   
    If xml.Status = 200 Then
       str1 = StrConv(xml.responseBody, vbUnicode) '返回的内容
       MsgBox str1
    End If
End Sub

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭