VB自动登陆网络站点详解(三):Internet Explorer对象

来源:互联网 发布:西南大学网络教学平台 编辑:程序博客网 时间:2024/04/27 17:06

 

VB自动登陆网络站点详解(三):Internet Explorer对象

使用Internet Explorer对象可以使POST请求在IE中产生,从而脱离程序,远离了美化界面的工作。程序甚至可以隐藏或退出,免去了WebBrowser带来的烦恼,当然也不需要第四个参数。

下面我们还是以登陆CSDN为例,给出实际的代码,您可以根据前面文章中提供的参数换成你注册过的站点:

建新工程,在工程中“引用”Internet Explorer对象,点“浏览”,在系统文件夹下找到Shdocvw.dll(这个文件是IE自带的), Form1中添加Command1,以下是代码——

 

    Dim g_oIE As InternetExplorer

Private Sub Command1_Click()

    Dim vPost As Variant

    Dim vHeaders As Variant

 

    Set g_oIE = New InternetExplorer

    g_oIE.Visible = True

   

    ReDim aByte(0) As Byte

    cPostData = "login_name=帐号&password=密码&cookietime=0"

    PackBytes aByte(), cPostData

   

    vPost = aByte

    vHeaders = "Content-Type: application/x-www-form-urlencoded" + Chr(10) + Chr(13)

   

    g_oIE.Navigate "http://www.csdn.net/member/logon.asp", , , vPost, vHeaders

   

End Sub

Private Sub PackBytes(ByteArray() As Byte, ByVal PostData As String)

    iNewBytes = Len(PostData) - 1

    If iNewBytes < 0 Then Exit Sub

    ReDim ByteArray(iNewBytes)

    For i = 0 To iNewBytes

       ch = Mid(PostData, i + 1, 1)

       If ch = Space(1) Then

          ch = "+"

       End If

       ByteArray(i) = Asc(ch)

    Next

End Sub

(请输入自己的帐号及密码试运行。这种方法的好处是显而易见的,你可以按这个方法将前面的代码改造一下。)

PackBytes函数将Post出去的数据转化为一个ASCII数组,另外vHeaders的值必须以“+ Chr(10) + Chr(13)”结束。

 

代码没有什么好解释的,现在已经进入到Shdocvw.dll这个“库”中去了,而前面所说的WebBrowser及Internet Explorer都是这个库中所包含的“类”。大家可以打开对象浏览器看看它们互相之间的关系。  
原创粉丝点击