Domanda

Esiste un modo per eseguire una richiesta GET HTTP all'interno di uno script di Visual Basic? Ho bisogno di ottenere il contenuto della risposta da un particolare URL per l'elaborazione.

È stato utile?

Soluzione

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.

Altri suggerimenti

Al momento in cui hai scritto non hai descritto cosa farai con la risposta o quale sia il suo tipo di contenuto. Una risposta contiene già un utilizzo di base di MSXML2.XMLHTTP (raccomando il progID più esplicito MSXML2.XMLHTTP.3.0 ), tuttavia potresti dover fare cose diverse con la risposta , potrebbe non essere testo.

XMLHTTP ha anche una proprietà responseBody che è una versione array di byte della risposta e c'è un responseStream che è un wrapper IStream per la risposta.

Nota che in un requisito lato server (ad es. VBScript ospitato in ASP) useresti MSXML.ServerXMLHTTP.3.0 o WinHttp.WinHttpRequest.5.1 (che ha un'interfaccia quasi identica).

Ecco un esempio dell'uso di XmlHttp per recuperare un file PDF e archiviarlo: -

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

Se si utilizza la richiesta GET per inviare effettivamente i dati ...

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

Il problema con MSXML2.XMLHTTP è che ne esistono diverse versioni, con nomi diversi a seconda della versione del sistema operativo Windows e delle patch.

questo lo spiega: http://support.microsoft.com/kb/269238

Ho avuto più fortuna usando vbscript per chiamare

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

.... e altre cose, ma solo per far passare la richiesta.

        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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top