Template:Data export/vbs

From Awesome Baozam
Revision as of 12:47, 8 February 2017 by Baya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
' This Sub downloads the FILE specified in myUrl to the file specified in mySaveTo.

Const myDevice = "<HOSTID>", myUser = "<USR>", myPwd = "<PASSWORD>"
Const myHost = "baozam.net", mySaveTo = "D:\baozam.csv"
Const myPeriod = "259200", mySTime = "0", myStep = "g1d"

HTTPDownload "https://" + myHost + "/index.php?action=csv&hosts=" + myDevice + "&period=" + myPeriod + "&stime=" + mySTime + "&step=" + myStep
Sub HTTPDownload( myUrl )

' Based on a script found on the Thai Visa forum
' http://www.thaivisa.com/forum/index.php?showtopic=21832
' by Rob van der Woude
' http://www.robvanderwoude.com/vbstech_internet_download.php

    ' Standard housekeeping
    Dim i, objFile, objFSO, objHTTP
    Const ForReading = 1, ForWriting = 2, ForAppending = 8

    ' Create a File System Object
    Set objFSO = CreateObject( "Scripting.FileSystemObject" )

    ' Create the target file
    Set objFile = objFSO.CreateTextFile( mySaveTo, True, True ) ' overwrite, unicode

    ' Create an HTTP object
    Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )

    ' Download the specified URL
    objHTTP.Open "POST", myUrl, False
    'objHTTP.Option(4) = &H3300 'WinHttpRequestOption_SslErrorIgnoreFlags = 0x3300      
    objHTTP.setRequestHeader "myHost", myHost
    objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    objHTTP.Send "name=" + myUser + "&password=" + myPwd

    ' Write the downloaded byte stream to the target file
    For i = 1 To LenB( objHTTP.ResponseBody )
        objFile.Write Chr( AscB( MidB( objHTTP.ResponseBody, i, 1 ) ) )
    Next

    ' Close the target file
    objFile.Close( )
End Sub