This article describes a module method of visual basic6.0, which uses XMLHTTP to implement the Post and Get functions. Although it is an old code, it can replace the Inet control to achieve data communication. It’s worth learning from.
The main module code is as follows:
'================================================== ========='| Module name | XMLHTTP'| Description | Replace the Inet control to achieve data communication'======================== ===================================Public Enum DataEnum ResponseText = 1 ResponseBody = 2End 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 '--------------------------------------------- Function returnsSelect Case DataStic Case ResponseText '-------------------------------- Directly return the string DataS = XMLHTTP.ResponseText GetData = DataS Case ResponseBody '-- ------------------------------- Directly return binary DataB = XMLHTTP.ResponseBody GetData = DataB Case ResponseBody + ResponseText '---- -------------------------- Binary to string conversion [try when the string returned directly is garbled] DataS = BytesToStr(XMLHTTP.ResponseBody) GetData = DataS Case Else '--------------------------------Invalid return GetData = "" End Select '----------------------------------------Release space Set XMLHTTP = Nothing Exit FunctionERR: 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 '--------------------------------Function returns Select Case DataStic Case ResponseText '----------------------------- Directly return the string DataS = XMLHTTP.ResponseText PostData = DataS Case ResponseBody '-- ------------------------------- Directly return binary DataB = XMLHTTP.ResponseBody PostData = DataB Case ResponseBody + ResponseText '---- ----------------------- Binary to string conversion [try when the string returned directly is garbled] DataS = BytesToStr(XMLHTTP.ResponseBody) PostData = DataS Case Else '--------------------------------Invalid return PostData = "" End Select '------------------------------------Release space Set XMLHTTP = Nothing Exit FunctionERR : 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 = strReturnEnd Function