This article describes a visual basic6.0 module method that uses XMLHTTP to implement post and get functions, although it is an old code, but can replace the inet control to achieve data communication. It is worth learning to learn from.
The main module code is as follows:
'==========================================================
'| Module Name | XMLHTTP ' | Description | Replace the inet control to implement data communications ' ==========================================================public Enum dataenum responsetext = 1 Resp Onsebody = 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.X Mlhttp ") XMLHTTP. Open ' Get ', URL, True xmlhttp.send while XMLHTTP. ReadyState <> 4 DoEvents wend '--------------------------------------function returns Select case datastic case RESP Onsetext '--------------------------------directly returns the string DataS = XMLHTTP. ResponseText GetData = DataS case Responsebody '--------------------------------directly returns binary Datab = XMLHTTP. Responsebody GetData = datab case responsebody + responsetext '------------------------------binary turn Strings [direct return string appears garbled Try] DataS = bytestostr (XMLHTtp. responsebody) GetData = DataS case Else '--------------------------------invalid return GetData = ' End Select ' --------------------------------------free space Set XMLHTTP = Nothing Exit function err:getdata = "" End Function Publ IC Function postdata (ByVal strurl As String, ByVal strdata As String, ByVal datastic as Dataenum) as Variant on Error go To 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-T" Ype "," application/x-www-form-urlencoded "Xmlhttp.send (strdata) do Until XMLHTTP. ReadyState = 4 DoEvents Loop '-----------------------------function returns Select case datastic case ResponseText '- ------------------------------directly returns the string DataS = XMLHTTP. ResponseText postdata = DataS case Responsebody '--------------------------------direct returnBack to binary Datab = XMLHTTP. Responsebody postdata = datab case responsebody + responsetext '---------------------------binary turn string [direct return strings appear garbled when trying to ] DataS = Bytestostr (XMLHTTP.
responsebody) PostData = DataS case Else '--------------------------------invalid return postdata = "" End Select '------------------------------------free space Set XMLHTTP = Nothing Exit function err:postdata = "" End Function Fun
Ction 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 = as CB (MidB (vIn, i + 1, 1)) Strreturn = Strreturn & Chr (CLng (thischarcode) * &h100 + CInt (nextcharcode)) I = i + 1 end If Next bytestostr = Strreturn End Function