1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
Sub Test_DownloadFile()
Dim strTargetURL As String
Dim strFilename As String
strFilename = "C:\Téléchargement\XML\ApiRest_Test.xml"
strTargetURL = "https://apirest.atinternet-solutions.com/data/v2/xml/getData?&columns={d_time_date,m_visits,m_visitors,d_site,d_page,m_page_loads,d_l2,d_page_chap1,d_page_chap2,d_page_chap3}&sort={-m_visits}&space={s:61074}&period={D:{start:'2015-01-01',end:'2015-01-01'}}&max-results=10"
Call DownloadFile(strFilename, strTargetURL, True)
End Sub
Sub DownloadFile(ByVal TargetFileName As String, ByVal URL As String, ByVal OverWrite As Boolean)
Const NO_OVERWRITE_FILE As Integer = 1
Const OVERWRITE_FILE As Integer = 2
On Error GoTo L_ErrDownloadFile
Dim WinHttpReq As Object
Dim oStream As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
With WinHttpReq
.Open "GET", URL, False, "username", "password"
.Send
End With
URL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
With oStream
.Open
.Type = 1
.Write WinHttpReq.responseBody
.SaveToFile TargetFileName, IIf(OverWrite, OVERWRITE_FILE, NO_OVERWRITE_FILE)
.Close
End With
Else
Err.Raise vbObjectError + WinHttpReq.Status, "Téléchargement échoué", "Erreur " & WinHttpReq.Status & " levée durant l'accès au site..." & vbCrLf & vbCrLf & "Sans doute une authentification est nécessaire pour accéder à la ressource..."
End If
On Error GoTo 0
L_ExDownloadFile:
Set oStream = Nothing
Set WinHttpReq = Nothing
Exit Sub
L_ErrDownloadFile:
MsgBox Err.Description, 48, Err.Source
Resume L_ExDownloadFile
End Sub |
Partager