Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Sub SaveFileFromURL()
  4.  
  5. Dim FileData() As Byte
  6. Dim FileNum As Long
  7. Dim WHTTP As Object
  8. Dim mainUrl As String, fileUrl As String, filePath As String, myuser As String, mypass As String
  9. Dim strAuthenticate As String
  10.  
  11. mainUrl = "https://secure.brandbank.com/login.aspx?"
  12. fileUrl = "https://productlibrary.brandbank.com/image/gallerylarge/3520198"
  13.  
  14. ' OR fileUrl = "https://productlibrary.brandbank.com/LabelImage/ViewLabel?pvid=3520198"
  15.  
  16. filePath = "D:DOWNLOADSSYStest.jpg"
  17.  
  18. ' dummy UN, PW
  19. myuser = "admin"
  20. mypass = "Password"
  21.  
  22. 'strAuthenticate = "start-url=%2F&user=" & myuser & "&password=" & mypass & "&switch=Log+In"
  23. 'strAuthenticate = "txtemail=myuser&txtpassword=mypass&cookieCheck=true"
  24.  
  25. Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
  26.  
  27. 'I figured out that you have to POST authentication string to the main website address not to the direct file address
  28. WHTTP.Option(6) = False 'WinHttpRequestOption_EnableRedirects
  29. WHTTP.Open "POST", mainUrl, False 'WHTTP.Open "POST", fileUrl, False
  30. 'WHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  31. WHTTP.SetRequestHeader "Content-Type", "multipart/form-data"
  32. 'WHTTP.Send strAuthenticate
  33.  
  34. 'Then you have to GET direct file url
  35. WHTTP.Open "GET", fileUrl, False, myuser, mypass
  36. WHTTP.Send
  37.  
  38. FileData = WHTTP.ResponseBody
  39. Set WHTTP = Nothing
  40.  
  41. 'Save the file
  42. FileNum = FreeFile
  43. Open filePath For Binary Access Write As #FileNum
  44. Put #FileNum, 1, FileData
  45. Close #FileNum
  46.  
  47. MsgBox "File has been saved!", vbInformation, "Success"
  48.  
  49. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement