VB uses XMLHTTP to realize post and get method _vb

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.