摘要:[VB6]自動登陸網路站點詳解(四):在WebBrowser中發送POST請求
本來這一部分內容也應該放在第二章,但一方面為了醒目,另一方面,這種方法實際上與Internet Explorer物件有很大的聯繫及相似性,所以特意將之放在Internet Explorer物件之後介紹。
現在我們要用到的也是WebBrowser的“Navigate”方法,其函數原型如下所示:
Sub Navigate(URL As String, [Flags], [TargetFrameName], [PostData], [Headers])
大家不妨與第三章中Internet Explorer物件的“Navigate”方法比較一下,一模一樣,原來是同一個介面!!
新建一個工程,部件中勾選中 “Microsoft Internet Controls”,添加一個WebBrowser1、一個Command1在表單上,可以把WebBrowser1適當拉大一點,Form1中添加以下代碼:
Private Sub Command1_Click()
ReDim aByte(0) As Byte ' Array of bytes to hold data to post
cPostData = "login_name=帳號&password=密碼&cookietime=0&x=42&y=10"
PackBytes aByte(), cPostData
Dim vPost As Variant
vPost = aByte ' Assign the byte array to a VARIANT
Dim vHeaders As Variant
vHeaders = "Content-Type: application/x-www-form-urlencoded" + Chr(10) + Chr(13)
WebBrowser1.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 ' Get rid of the null termination
If iNewBytes < 0 Then
Exit Sub
End If
ReDim ByteArray(iNewBytes)
For i = 0 To iNewBytes
ch = Mid(PostData, i + 1, 1)
If ch = Space(1) Then
ch = "+"
End If
Debug.Print ch, Asc(ch)
ByteArray(i) = Asc(ch)
Next
End Sub
(請參考第三章中的代碼。)
通過這四篇文章的介紹,我想讀者一定不光是對VB登陸Web伺服器有了更深的認識,而且同時對HTTP協議、Cookie、Session也加深了理解!
全文完!!!