【轉】介紹vb使用xmlhttp對象、webbrowser控件、inet控件進行Post發包、Get發包的方法


第二章

 

先發一段數據,我們來分析它<登陸百度的包>

POST /?login HTTP/1.1Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*

Referer: https://passport.baidu.com/?login&tpl=mn

Accept-Language: zh-cn

Content-Type: application/x-www-form-urlencoded

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 663; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)

Host: passport.baidu.com

Content-Length: 236

Connection: Keep-Alive

Cache-Control: no-cache

 

Cookie:

tpl_ok=&next_target=&tpl=mn&skip_ok=&aid=&need_pay=&need_coin=&pay_method=&u=http%3A%2F%2Fwww.baidu.com%2F&return_method=get&more_param=&return_type=&psp_tt=0&password=123465&safeflg=0&username=sunshinebean&verifycode=&mem_pass=on

 

 

這里主要講Post包的構成及比較重要的Http頭參數

 

1. Http頭里的Referer參數,簡單的說就是來路,然后目標服務器能知道你這個Http請求是哪個頁面請求過去的,有些服務器就是判斷來路的所以這個參數比較重要

 

2. Http頭里的Content-Type參數,提交的是網頁信息可以是application/x-www-form-urlencoded,假如提交圖片信息這個參數也會隨之變成data

 

3. Post的包參數全部用&符號隔開的,=號后面的是參數的值。有些參數是固定不變的有些卻是隨機改變的,這些參數80%能在網頁源碼里找 到,10%能在cookie里找到,10%能在JS里構造(這個比較麻煩要會分析JS),在上面這段數據里變動的就帳號跟密碼,別的參數都是固定的,當然 這個結論要多次分析獲得。比如這里的包, username=sunshinebean,password=123465就對應百度的帳號跟密碼

 

 

VB創建Xmlhttp對象

Private Function GetHtmlStr$(StrUrl$, switch%) ‘獲取源碼
Dim XmlHttp
Set XmlHttp = CreateObject("Microsoft.XMLHTTP")
XmlHttp.Open "GET", StrUrl, True
XmlHttp.send
stime = Now ‘獲取當前時間
While XmlHttp.ReadyState <> 4
DoEvents
ntime = Now ‘獲取循環時間
If DateDiff("s", stime, ntime) > 3 Then GetHtmlStr = "": Exit Function ‘判斷超出3秒即超時退出過程
Wend
GetHtmlStr = StrConv(XmlHttp.ResponseBody, vbUnicode)
Set XmlHttp = Nothing
End Function

 

Private Function GetHtmlStr$(StrUrl$, switch%) ‘獲取源碼

Dim XmlHttp

Set XmlHttp = CreateObject("Microsoft.XMLHTTP")

XmlHttp.Open "GET", StrUrl, True

XmlHttp.send

stime = Now ‘獲取當前時間

While XmlHttp.ReadyState <> 4

DoEvents

ntime = Now ‘獲取循環時間

If DateDiff("s", stime, ntime) > 3 Then GetHtmlStr = "": Exit Function ‘判斷超出3秒即超時退出過程

Wend

GetHtmlStr = StrConv(XmlHttp.ResponseBody, vbUnicode)

Set XmlHttp = Nothing

End Function

這個是我自己寫的一個函數,主要作用是獲取指定網頁的源碼

 

XmlHttp.Open "GET", StrUrl, True

 

"GET"是數據的請求方式,還有種就是POST提交數據寫成"POST"

 

StrUrl是指定的網址,GET請求時寫要GET的目的網址,POST時寫提交的目的地址

 

True是異步,False是同步,區別就是同步是等待數據全部獲取后再顯示,所以會卡,而異步則不會卡,所以推薦用異步比較好

 

XmlHttp.setRequestHeader "Referer", RefUrl

 

指定來路,上章已經提到過Referer參數的重要性了, RefUrl就寫截包截來的那個來路

 

XmlHttp.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"

 

這個上章也提到過,是類型,一般都是按截的包里面的寫,網頁的話直接寫成這樣就好:"application/x-www-form-urlencoded"

 

XmlHttp.send "XXX"

 

這里的XXX就是post的包內容,Get的話這個是空的,POST的話把包的內容寫在這里

 

然后該函數返回POST的返回信息,我們一般可以在返回值里提取出特定的東西來判斷執行某樣東西是否成功

 

二:webbrowser

 

先看段代碼:

 

Private Sub Command1_Click()

 

Dim URL$, Flags&, TargetFrame$, PostData() As Byte, Headers$

 

URL = "https://passport.baidu.com/?login"

 

Flags = 0

 

TargetFrame = ""

 

PostData = "tpl_ok=&next_target=&tpl=mn&skip_ok=&aid=&need_pay=&need_coin=&pay_method=&u=http%3A%2F%2Fwww.baidu.com%2F&return_method=get&more_param=&return_type=&psp_tt=0&password=123456&safeflg=0&username=sunshinebean&verifycode="

 

PostData = StrConv(PostData, vbFromUnicode)

 

Headers = "Content-Type: application/x-www-form-urlencoded" & vbCrLf

 

WebBrowser1.Navigate URL, Flags, TargetFrame, PostData, Headers

 

End Sub

 

Private Sub Form_Load()

 

WebBrowser1.Navigate "http://baidu.com"

 

End Sub

 

Webbrowser有個Navigate方法,參數是這樣的:

 

object.Navigate URL [Flags,] [TargetFrameName,] [PostData,] [Headers]

 

拿出MSDN,查之,見下頁:

 

URL:

 

Required. A string expression that evaluates to the URL, full path, or Universal Naming Convention (UNC) location and name of the resource to display.

 

Flags:

 

Optional. A constant or value that specifies whether to add the resource to the history list, whether to read from or write to the cache, and whether to display the resource in a new window. It can be a combination of the following constants or values. Constant Value Meaning

 

navOpenInNewWindow 1 Open the resource or file in a new window.

 

navNoHistory 2 Do not add the resource or file to the history list. The new page replaces the current page in the list.

 

navNoReadFromCache 4 Do not read from the disk cache for this navigation.

 

navNoWriteToCache 8 Do not write the results of this navigation to the disk cache.

 

TargetFrameName:

 

Optional. String expression that evaluates to the name of an HTML frame in URL to display in the browser window. The possible values for this parameter are: _blank Load the link into a new unnamed window.

 

_parent Load the link into the immediate parent of the document the link is in.

 

_self Load the link into the same window the link was clicked in.

 

_top Load the link into the full body of the current window.

 

<window_name> A named HTML frame. If no frame or window exists that matches the specified target name, a new window is opened for the specified link.

 

PostData:

 

Optional. Data to send to the server during the HTTP POST transaction. For example, the POST transaction is used to send data gathered by an HTML form to a program or script. If this parameter does not specify any post data, the Navigate method issues an HTTP GET transaction. This parameter is ignored if URL is not an HTTP URL.

 

Headers

 

Optional. A value that specifies additional HTTP headers to send to the server. These headers are added to the default Internet Explorer headers. The headers can specify things like the action required of the server, the type of data being passed to the server, or a status code. This parameter is ignored if URL is not an HTTP URL.

 

URL 指定需要使用的網頁。

 

flags 指定是否將該資源添加到歷史列表、或通過高速緩存讀寫,將該資源顯示在一個新窗口中、或這些方式的組合。

 

targetframename 指定目標顯示區的名稱。

 

postdata 指定需要發送到 HTTP的Post的數據。

 

headers 指定需要發送的 HTTP 標題。

 

整合了下就是這樣。PostData構造方法一樣,就是要特別注意的是數據要轉成vbFromUnicode才能提交

 

三,INET控件

 

我想這個控件大家應該不陌生吧,很多VB寫的程序啊軟件都用到了這個控件,這家伙封裝了Http和Ftp協議使用起來很方便所以用的人很多。代碼也很簡潔呵呵~~

 

Dim PostDate As String

 

If Inet1.StillExecuting = True Then Exit Sub

 

PostDate = "tpl_ok=&next_target=&tpl=mn&skip_ok=&aid=&need_pay=&need_coin=&pay_method=&u=http%3A%2F%2Fwww.baidu.com%2F&return_method=get&more_param=&return_type=&psp_tt=0&password=123456&safeflg=0&username=sunshinebean&verifycode="

 

Inet1.Execute "https://passport.baidu.com/?login", "POST", PostDate, "Referer: https://passport.baidu.com/?login&tpl=mn" & vbCrLf & "Content-Type: application/x-www-form-urlencoded"

 

Inet和xmlhttp的好處就是能提交Referer

 

Inet的Execute方法: 

 

URL是Post的目標地址, operation是Get方法或者Post方法,data 是Post的數據, requestHeader是Referer來路和Content-Type

 

下面講下數據的返回:

 

在inet的StateChanged事件里返回數據,判斷state是不是12,原因:

 

0 未報告狀態icHostResolvingHost

 

1 控件正在尋找指定主機的IP地址icHostResolved

 

2 控件已成功找到指定主機的IP地址icConnecting

 

3 控件正在與指定主機進行連接icConnected

 

4 控件已成功與指定主機連接icRequesting

 

5 控件正在向主機發出請求icRequestSent

 

6 控件已成功向主機發出請求icReceivingResponse

 

7 控件正在從主機接收反饋信息icResponseReceived

 

8 控件已成功從主機接受反饋信息icDisconnecting

 

9 控件正在與主機斷開icDisconnected

 

10 控件已與主機斷開icError

 

11 在與主機通信的過程中發生了錯誤icResponseCompleted

 

12 請求結束且數據已經接收到

 

Dim strData$

 

Dim bDone As Boolean: bDone = False

 

vtData = Inet1.GetChunk(1024, icString)

 

Do While Not bDone

 

strData = strData & vtData

 

DoEvents

 

vtData = Inet1.GetChunk(1024, icString)

 

If Len(vtData) = 0 Then

 

bDone = True

 

End If

 

Loop

 

這里返回string類型的源碼。。要是二進制或者UTF8的話還要簡單

 

定義一個byte數組就行了

 

Dim Buff() As Byte

 

Buff = Inet1.GetChunk(0, icByteArray)

 

獲取到的圖圖保存在路徑下面再用picture加載就是圖圖了。UTF8的源碼用解碼函數進行解碼即可解決亂碼的問題,UTF8解碼函數:

 

Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long

 

Private Const CP_UTF8 = 65001

 

Function Utf8ToUnicode(ByRef Utf() As Byte) As String

 

Dim lRet As Long

 

Dim lLength As Long

 

Dim lBufferSize As Long

 

lLength = UBound(Utf) – LBound(Utf) + 1

 

If lLength <= 0 Then Exit Function

 

lBufferSize = lLength * 2

 

Utf8ToUnicode = String$(lBufferSize, Chr(0))

 

lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf(0)), lLength, StrPtr(Utf8ToUnicode), lBufferSize)

 

If lRet <> 0 Then

 

Utf8ToUnicode = Left(Utf8ToUnicode, lRet)

 

Else

 

Utf8ToUnicode = ""

 

End If

 

End Function

 

調用Utf8ToUnicode(Buff)即可!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM