Advertisement
Guest User

Untitled

a guest
Feb 21st, 2016
4,336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. Public Sub download_SCData_test()
  2. On Error GoTo err_me
  3. Dim fData '() As Byte
  4. Dim count As Long
  5. Dim fileNum As Long
  6. Dim ado_strm As Object
  7. Dim winHTTP As Object 'New winHTTP.WinHttpRequest 'or Set winHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
  8. Dim destPath As String
  9. Dim destPath2 As String
  10. Dim fileURL As String
  11. Dim mainURL1 As String
  12. Dim mainURL2 As String
  13.  
  14. ' HttpRequest SetCredentials flags
  15. ' It might also be necessary to supply credentials to the proxy if you connect to the Internet through a proxy that requires authentication.
  16. Const CREDENTIALS_FOR_SERVER = 0
  17. Const CREDENTIALS_FOR_PROXY = 1
  18. Const HTTPREQUEST_PROXYSETTING_PROXY = 2
  19.  
  20. Set winHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
  21.  
  22. mainURL1 = "http://wsso.someplace.on.the.web:XXXX/redirect.html?URL=https://someplace.on.the.web/quality/data_metrics/mac/"
  23. mainURL2 = "http://wsso.someplace.on.the.web:XXXX/redirect.html?URL=https://someplace.on.the.web/quality/data_metrics/mac/"
  24.  
  25. 'fileURL = "http://wsso-support.web.boeing.com:XXXX/redirect.html?URL=https://someplace.on.the.web/quality/data_metrics/mac/data.xlsb"
  26. fileURL = "\serverXXXwebdataqualitydata_metricsmacdata.xlsb"
  27.  
  28. destPath = "C:Tempdata.xlsb"
  29. destPath2 = "C:Tempdata2.xlsb"
  30.  
  31. With winHTTP
  32. .SetProxy proxysetting:=HTTPREQUEST_PROXYSETTING_PROXY, ProxyServer:="wsso.someplace.on.the.web:XXXX", BypassList:="*.someplace.on.the.web"
  33.  
  34. .Option(Option:=WinHttpRequestOption_SslErrorIgnoreFlags) = 13056
  35. .Option(Option:=WinHttpRequestOption_MaxAutomaticRedirects) = 20 'default 10
  36. .Option(Option:=WinHttpRequestOption_EnableHttpsToHttpRedirects) = True
  37. .Option(Option:=WinHttpRequestOption_EnableRedirects) = True
  38. .Option(Option:=WinHttpRequestOption_RevertImpersonationOverSsl) = True
  39.  
  40. .SetTimeouts 30000, 30000, 30000, 30000 'ms - resolve, connect, send, receive
  41.  
  42. ' Send a request to the server and wait for a response.
  43. 'POST authentication string to the main website address not to the direct file address
  44. .Open Method:="POST", URL:=mainURL1, async:=False
  45.  
  46. '.SetCredentials UserName:="serveruser", Password:="pass", Flags:=CREDENTIALS_FOR_SERVER ' this line has no effect
  47.  
  48. 'strAuthenticate = "start-url=%2F&user=" & myuser & "&password=" & mypass & "&switch=Log+In"
  49.  
  50. .setRequestHeader Header:="Content-Type", Value:="application/x-www-form-urlencoded"
  51. .setRequestHeader Header:="Date", Value:=Date
  52.  
  53. .send 'body:=strAuthenticate
  54. If Not .WaitForResponse(TimeOut:=30000) Then MsgBox "timeout!": GoTo exit_me
  55.  
  56. Sleep 2000
  57.  
  58. .Open Method:="POST", URL:=mainURL2, async:=False
  59. .send 'body:=strAuthenticate
  60. If Not .WaitForResponse(TimeOut:=30000) Then MsgBox "timeout!": GoTo exit_me
  61.  
  62. Sleep 2000
  63.  
  64. .Open Method:="GET", URL:=fileURL, async:=True
  65. .send
  66.  
  67. If Not .WaitForResponse(TimeOut:=30000) Then MsgBox "timeout!": GoTo exit_me
  68.  
  69. Sleep 2000
  70.  
  71. Do While InStr(1, .responseText, "function WSSORedirect()", vbTextCompare)
  72. Sleep 2000
  73. count = count + 1: If count > 2 Then Exit Do
  74.  
  75. Debug.Print InStr(1, .responseText, "function WSSORedirect()", vbTextCompare)
  76.  
  77. If InStr(1, .responseText, "function WSSORedirect()", vbTextCompare) < 1 Then MsgBox "any luck?"
  78.  
  79. .Open Method:="GET", URL:=fileURL, async:=True
  80. .send
  81. If Not .WaitForResponse(TimeOut:=30000) Then MsgBox "timeout!": GoTo exit_me
  82.  
  83.  
  84. Debug.Print count
  85. Sleep 2000
  86. Loop
  87.  
  88. Sleep 2000
  89.  
  90. fData = .responseBody
  91.  
  92. ' Display the results of the request.
  93. Debug.Print "Credentials: "
  94. Debug.Print .Status & " " & .StatusText
  95. Debug.Print .getAllResponseHeaders
  96. End With
  97.  
  98.  
  99. If Dir(destPath) <> vbNullString Then Kill destPath
  100.  
  101. fileNum = FreeFile
  102. Open destPath For Binary Access Write As #fileNum
  103. Put #fileNum, 1, fData
  104. Close #fileNum
  105.  
  106.  
  107. If Dir(destPath2) <> vbNullString Then Kill destPath2
  108.  
  109. Set strm = CreateObject("ADODB.Stream")
  110. With strm
  111. .Type = 1
  112. .Open
  113. .Write winHTTP.responseBody
  114. .SaveToFile destPath2, 2 'overwrite
  115. End With
  116.  
  117. MsgBox "Completed. Check 'C:Temp'.", vbInformation, "execution completed"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement