VB應用XMLHTTP完成Post與Get的辦法。本站提示廣大學習愛好者:(VB應用XMLHTTP完成Post與Get的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是VB應用XMLHTTP完成Post與Get的辦法正文
本文所述為visual basic6.0的一個模塊辦法,是應用XMLHTTP完成Post與Get功效,固然是一個老代碼,然則可以替換Inet控件,完成數據通信。很值得進修自創一下。
重要模塊代碼以下:
'========================================================== '| 模 塊 名 | XMLHTTP '| 說 明 | 替換Inet控件,完成數據通信 '==========================================================Public Enum DataEnum ResponseText = 1 ResponseBody = 2 End Enum Public Function GetData(ByVal Url As String, ByVal DataStic As DataEnum) As Variant On Error GoTo ERR: Dim XMLHTTP As Object Dim DataS As String Dim DataB() As Byte Set XMLHTTP = CreateObject("Microsoft.XMLHTTP") XMLHTTP.Open "get", Url, True XMLHTTP.send While XMLHTTP.ReadyState <> 4 DoEvents Wend '--------------------------------------函數前往 Select Case DataStic Case ResponseText '--------------------------------直接前往字符串 DataS = XMLHTTP.ResponseText GetData = DataS Case ResponseBody '--------------------------------直接前往二進制 DataB = XMLHTTP.ResponseBody GetData = DataB Case ResponseBody + ResponseText '------------------------------二進制轉字符串[直接前往字串湧現亂碼時測驗考試] DataS = BytesToStr(XMLHTTP.ResponseBody) GetData = DataS Case Else '--------------------------------有效的前往 GetData = "" End Select '--------------------------------------釋放空間 Set XMLHTTP = Nothing Exit Function ERR: GetData = "" End Function Public Function PostData(ByVal StrUrl As String, ByVal StrData As String, ByVal DataStic As DataEnum) As Variant On Error GoTo ERR: Dim XMLHTTP As Object Dim DataS As String Dim DataB() As Byte Set XMLHTTP = CreateObject("Microsoft.XMLHTTP") XMLHTTP.Open "POST", StrUrl, True XMLHTTP.setRequestHeader "Content-Length", Len(PostData) XMLHTTP.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded" XMLHTTP.send (StrData) Do Until XMLHTTP.ReadyState = 4 DoEvents Loop '-----------------------------函數前往 Select Case DataStic Case ResponseText '--------------------------------直接前往字符串 DataS = XMLHTTP.ResponseText PostData = DataS Case ResponseBody '--------------------------------直接前往二進制 DataB = XMLHTTP.ResponseBody PostData = DataB Case ResponseBody + ResponseText '---------------------------二進制轉字符串[直接前往字串湧現亂碼時測驗考試] DataS = BytesToStr(XMLHTTP.ResponseBody) PostData = DataS Case Else '--------------------------------有效的前往 PostData = "" End Select '------------------------------------釋放空間 Set XMLHTTP = Nothing Exit Function ERR: PostData = "" End Function Function BytesToStr(ByVal vIn) As String strReturn = "" For i = 1 To LenB(vIn) ThisCharCode = AscB(MidB(vIn, i, 1)) If ThisCharCode < &H80 Then strReturn = strReturn & Chr(ThisCharCode) Else NextCharCode = AscB(MidB(vIn, i + 1, 1)) strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode)) i = i + 1 End If Next BytesToStr = strReturn End Function