HTTP GET in VBS

前端 未结 4 1730
北荒
北荒 2020-11-27 03:51

Is there a way to perform an HTTP GET request within a Visual Basic script? I need to get the contents of the response from a particular URL for processing.

相关标签:
4条回答
  • 2020-11-27 04:16

    You haven't at time of writing described what you are going to do with the response or what its content type is. An answer already contains a very basic usage of MSXML2.XMLHTTP (I recommend the more explicit MSXML2.XMLHTTP.3.0 progID) however you may need to do different things with the response, it may not be text.

    The XMLHTTP also has a responseBody property which is a byte array version of the reponse and there is a responseStream which is an IStream wrapper for the response.

    Note that in a server-side requirement (e.g., VBScript hosted in ASP) you would use MSXML.ServerXMLHTTP.3.0 or WinHttp.WinHttpRequest.5.1 (which has a near identical interface).

    Here is an example of using XmlHttp to fetch a PDF file and store it:-

    Dim oXMLHTTP
    Dim oStream
    
    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
    
    oXMLHTTP.Open "GET", "http://someserver/folder/file.pdf", False
    oXMLHTTP.Send
    
    If oXMLHTTP.Status = 200 Then
        Set oStream = CreateObject("ADODB.Stream")
        oStream.Open
        oStream.Type = 1
        oStream.Write oXMLHTTP.responseBody
        oStream.SaveToFile "c:\somefolder\file.pdf"
        oStream.Close
    End If
    
    0 讨论(0)
  • 2020-11-27 04:18
            strRequest = "<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" " &_
             "xmlns:tem=""http://tempuri.org/"">" &_
             "<soap:Header/>" &_
             "<soap:Body>" &_
                "<tem:Authorization>" &_
                    "<tem:strCC>"&1234123412341234&"</tem:strCC>" &_
                    "<tem:strEXPMNTH>"&11&"</tem:strEXPMNTH>" &_
                    "<tem:CVV2>"&123&"</tem:CVV2>" &_
                    "<tem:strYR>"&23&"</tem:strYR>" &_
                    "<tem:dblAmount>"&1235&"</tem:dblAmount>" &_
                "</tem:Authorization>" &_
            "</soap:Body>" &_
            "</soap:Envelope>"
    
            EndPointLink = "http://www.trainingrite.net/trainingrite_epaysystem" &_
                    "/trainingrite_epaysystem/tr_epaysys.asmx"
    
    
    
    dim http
    set http=createObject("Microsoft.XMLHTTP")
    http.open "POST",EndPointLink,false
    http.setRequestHeader "Content-Type","text/xml"
    
    msgbox "REQUEST : " & strRequest
    http.send strRequest
    
    If http.Status = 200 Then
    'msgbox "RESPONSE : " & http.responseXML.xml
    msgbox "RESPONSE : " & http.responseText
    responseText=http.responseText
    else
    msgbox "ERRCODE : " & http.status
    End If
    
    Call ParseTag(responseText,"AuthorizationResult")
    
    Call CreateXMLEvidence(responseText,strRequest)
    
    'Function to fetch the required message from a TAG
    Function ParseTag(ResponseXML,SearchTag)
    
     ResponseMessage=split(split(split(ResponseXML,SearchTag)(1),"</")(0),">")(1)
     Msgbox ResponseMessage
    
    End Function
    
    'Function to create XML test evidence files
    Function CreateXMLEvidence(ResponseXML,strRequest)
    
     Set fso=createobject("Scripting.FileSystemObject")
     Set qfile=fso.CreateTextFile("C:\Users\RajkumarJoshua\Desktop\DCIM\SampleResponse.xml",2)
     Set qfile1=fso.CreateTextFile("C:\Users\RajkumarJoshua\Desktop\DCIM\SampleReuest.xml",2)
    
     qfile.write ResponseXML
     qfile.close
    
     qfile1.write strRequest
     qfile1.close
    
    End Function
    
    0 讨论(0)
  • 2020-11-27 04:19

    If you are using the GET request to actually SEND data...

    check: http://techhelplist.com/index.php/tech-tutorials/37-windows-troubles/60-vbscript-sending-get-request

    The problem with MSXML2.XMLHTTP is that there are several versions of it, with different names depending on the windows os version and patches.

    this explains it: http://support.microsoft.com/kb/269238

    i have had more luck using vbscript to call

    set ID = CreateObject("InternetExplorer.Application")
    IE.visible = 0
    IE.navigate "http://example.com/parser.php?key=" & value & "key2=" & value2 
    do while IE.Busy.... 
    

    ....and more stuff but just to let the request go thru.

    0 讨论(0)
  • 2020-11-27 04:29
    Dim o
    Set o = CreateObject("MSXML2.XMLHTTP")
    o.open "GET", "http://www.example.com", False
    o.send
    ' o.responseText now holds the response as a string.
    
    0 讨论(0)
提交回复
热议问题