介紹vb使用xmlhttp對(duì)象、webbrowser控件、inet控件進(jìn)行Post發(fā)包、Get發(fā)包的方法
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
第二章 先發(fā)一段數(shù)據(jù),我們來(lái)分析它<登陸百度的包> POST /?login HTTP/1.1 Accept: 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 關(guān)于Http頭的構(gòu)成我不闡述,詳見(jiàn): http://hi.baidu.com/absky_cxb/blog/item/f28065017032760a738da5cb.html 這里主要講Post包的構(gòu)成及比較重要的Http頭參數(shù) 1. Http頭里的Referer參數(shù),簡(jiǎn)單的說(shuō)就是來(lái)路,然后目標(biāo)服務(wù)器能知道你這個(gè)Http請(qǐng)求是哪個(gè)頁(yè)面請(qǐng)求過(guò)去的,有些服務(wù)器就是判斷來(lái)路的所以這個(gè)參數(shù)比較重要 2. Http頭里的Content-Type參數(shù),提交的是網(wǎng)頁(yè)信息可以是application/x-www-form-urlencoded,假如提交圖片信息這個(gè)參數(shù)也會(huì)隨之變成data 3. Post的包參數(shù)全部用&符號(hào)隔開(kāi)的,=號(hào)后面的是參數(shù)的值。有些參數(shù)是固定不變的有些卻是隨機(jī)改變的,這些參數(shù)80%能在網(wǎng)頁(yè)源碼里找到,10%能在cookie里找到,10%能在JS里構(gòu)造(這個(gè)比較麻煩要會(huì)分析JS),在上面這段數(shù)據(jù)里變動(dòng)的就帳號(hào)跟密碼,別的參數(shù)都是固定的,當(dāng)然這個(gè)結(jié)論要多次分析獲得。比如這里的包, username=sunshinebean,password=123465就對(duì)應(yīng)百度的帳號(hào)跟密碼 第三章 一:VB創(chuàng)建Xmlhttp對(duì)象 Private Function GetHtmlStr$(StrUrl$, switch%) ‘獲取源碼 Dim XmlHttp Set XmlHttp = CreateObject("Microsoft.XMLHTTP") XmlHttp.Open "GET", StrUrl, True XmlHttp.send stime = Now ‘獲取當(dāng)前時(shí)間 While XmlHttp.ReadyState <> 4 DoEvents ntime = Now ‘獲取循環(huán)時(shí)間 If DateDiff("s", stime, ntime) > 3 Then GetHtmlStr = "": Exit Function ‘判斷超出3秒即超時(shí)退出過(guò)程 Wend GetHtmlStr = StrConv(XmlHttp.ResponseBody, vbUnicode) Set XmlHttp = Nothing End Function 這個(gè)是我自己寫(xiě)的一個(gè)函數(shù),主要作用是獲取指定網(wǎng)頁(yè)的源碼 XmlHttp.Open "GET", StrUrl, True "GET"是數(shù)據(jù)的請(qǐng)求方式,還有種就是POST提交數(shù)據(jù)寫(xiě)成"POST" StrUrl是指定的網(wǎng)址,GET請(qǐng)求時(shí)寫(xiě)要GET的目的網(wǎng)址,POST時(shí)寫(xiě)提交的目的地址 True是異步,F(xiàn)alse是同步,區(qū)別就是同步是等待數(shù)據(jù)全部獲取后再顯示,所以會(huì)卡,而異步則不會(huì)卡,所以推薦用異步比較好 XmlHttp.setRequestHeader "Referer", RefUrl 指定來(lái)路,上章已經(jīng)提到過(guò)Referer參數(shù)的重要性了, RefUrl就寫(xiě)截包截來(lái)的那個(gè)來(lái)路 XmlHttp.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded" 這個(gè)上章也提到過(guò),是類型,一般都是按截的包里面的寫(xiě),網(wǎng)頁(yè)的話直接寫(xiě)成這樣就好:"application/x-www-form-urlencoded" XmlHttp.send "XXX" 這里的XXX就是post的包內(nèi)容,Get的話這個(gè)是空的,POST的話把包的內(nèi)容寫(xiě)在這里 然后該函數(shù)返回POST的返回信息,我們一般可以在返回值里提取出特定的東西來(lái)判斷執(zhí)行某樣?xùn)|西是否成功 二: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有個(gè)Navigate方法,參數(shù)是這樣的: object.Navigate URL [Flags,] [TargetFrameName,] [PostData,] [Headers] 拿出MSDN,查之,見(jiàn)下頁(yè): 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 指定需要使用的網(wǎng)頁(yè)。 flags 指定是否將該資源添加到歷史列表、或通過(guò)高速緩存讀寫(xiě),將該資源顯示在一個(gè)新窗口中、或這些方式的組合。 targetframename 指定目標(biāo)顯示區(qū)的名稱。 postdata 指定需要發(fā)送到 HTTP的Post的數(shù)據(jù)。 headers 指定需要發(fā)送的 HTTP 標(biāo)題。 整合了下就是這樣。PostData構(gòu)造方法一樣,就是要特別注意的是數(shù)據(jù)要轉(zhuǎn)成vbFromUnicode才能提交 三,INET控件 我想這個(gè)控件大家應(yīng)該不陌生吧,很多VB寫(xiě)的程序啊軟件都用到了這個(gè)控件,這家伙封裝了Http和Ftp協(xié)議使用起來(lái)很方便所以用的人很多。代碼也很簡(jiǎn)潔呵呵~~ 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的目標(biāo)地址, operation是Get方法或者Post方法,data 是Post的數(shù)據(jù), requestHeader是Referer來(lái)路和Content-Type 下面講下數(shù)據(jù)的返回: 在inet的StateChanged事件里返回?cái)?shù)據(jù),判斷state是不是12,原因: 0 未報(bào)告狀態(tài)icHostResolvingHost 1 控件正在尋找指定主機(jī)的IP地址icHostResolved 2 控件已成功找到指定主機(jī)的IP地址icConnecting 3 控件正在與指定主機(jī)進(jìn)行連接icConnected 4 控件已成功與指定主機(jī)連接icRequesting 5 控件正在向主機(jī)發(fā)出請(qǐng)求icRequestSent 6 控件已成功向主機(jī)發(fā)出請(qǐng)求icReceivingResponse 7 控件正在從主機(jī)接收反饋信息icResponseReceived 8 控件已成功從主機(jī)接受反饋信息icDisconnecting 9 控件正在與主機(jī)斷開(kāi)icDisconnected 10 控件已與主機(jī)斷開(kāi)icError 11 在與主機(jī)通信的過(guò)程中發(fā)生了錯(cuò)誤icResponseCompleted 12 請(qǐng)求結(jié)束且數(shù)據(jù)已經(jīng)接收到 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類型的源碼。。要是二進(jìn)制或者UTF8的話還要簡(jiǎn)單 定義一個(gè)byte數(shù)組就行了 Dim Buff() As Byte Buff = Inet1.GetChunk(0, icByteArray) 獲取到的圖圖保存在路徑下面再用picture加載就是圖圖了。UTF8的源碼用解碼函數(shù)進(jìn)行解碼即可解決亂碼的問(wèn)題,UTF8解碼函數(shù): 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 調(diào)用Utf8ToUnicode(Buff)即可! 該文章在 2014/3/24 12:51:13 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |