Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Sub download_SCData_test()
- On Error GoTo err_me
- Dim fData '() As Byte
- Dim count As Long
- Dim fileNum As Long
- Dim ado_strm As Object
- Dim winHTTP As Object 'New winHTTP.WinHttpRequest 'or Set winHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
- Dim destPath As String
- Dim destPath2 As String
- Dim fileURL As String
- Dim mainURL1 As String
- Dim mainURL2 As String
- ' HttpRequest SetCredentials flags
- ' It might also be necessary to supply credentials to the proxy if you connect to the Internet through a proxy that requires authentication.
- Const CREDENTIALS_FOR_SERVER = 0
- Const CREDENTIALS_FOR_PROXY = 1
- Const HTTPREQUEST_PROXYSETTING_PROXY = 2
- Set winHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
- mainURL1 = "http://wsso.someplace.on.the.web:XXXX/redirect.html?URL=https://someplace.on.the.web/quality/data_metrics/mac/"
- mainURL2 = "http://wsso.someplace.on.the.web:XXXX/redirect.html?URL=https://someplace.on.the.web/quality/data_metrics/mac/"
- 'fileURL = "http://wsso-support.web.boeing.com:XXXX/redirect.html?URL=https://someplace.on.the.web/quality/data_metrics/mac/data.xlsb"
- fileURL = "\serverXXXwebdataqualitydata_metricsmacdata.xlsb"
- destPath = "C:Tempdata.xlsb"
- destPath2 = "C:Tempdata2.xlsb"
- With winHTTP
- .SetProxy proxysetting:=HTTPREQUEST_PROXYSETTING_PROXY, ProxyServer:="wsso.someplace.on.the.web:XXXX", BypassList:="*.someplace.on.the.web"
- .Option(Option:=WinHttpRequestOption_SslErrorIgnoreFlags) = 13056
- .Option(Option:=WinHttpRequestOption_MaxAutomaticRedirects) = 20 'default 10
- .Option(Option:=WinHttpRequestOption_EnableHttpsToHttpRedirects) = True
- .Option(Option:=WinHttpRequestOption_EnableRedirects) = True
- .Option(Option:=WinHttpRequestOption_RevertImpersonationOverSsl) = True
- .SetTimeouts 30000, 30000, 30000, 30000 'ms - resolve, connect, send, receive
- ' Send a request to the server and wait for a response.
- 'POST authentication string to the main website address not to the direct file address
- .Open Method:="POST", URL:=mainURL1, async:=False
- '.SetCredentials UserName:="serveruser", Password:="pass", Flags:=CREDENTIALS_FOR_SERVER ' this line has no effect
- 'strAuthenticate = "start-url=%2F&user=" & myuser & "&password=" & mypass & "&switch=Log+In"
- .setRequestHeader Header:="Content-Type", Value:="application/x-www-form-urlencoded"
- .setRequestHeader Header:="Date", Value:=Date
- .send 'body:=strAuthenticate
- If Not .WaitForResponse(TimeOut:=30000) Then MsgBox "timeout!": GoTo exit_me
- Sleep 2000
- .Open Method:="POST", URL:=mainURL2, async:=False
- .send 'body:=strAuthenticate
- If Not .WaitForResponse(TimeOut:=30000) Then MsgBox "timeout!": GoTo exit_me
- Sleep 2000
- .Open Method:="GET", URL:=fileURL, async:=True
- .send
- If Not .WaitForResponse(TimeOut:=30000) Then MsgBox "timeout!": GoTo exit_me
- Sleep 2000
- Do While InStr(1, .responseText, "function WSSORedirect()", vbTextCompare)
- Sleep 2000
- count = count + 1: If count > 2 Then Exit Do
- Debug.Print InStr(1, .responseText, "function WSSORedirect()", vbTextCompare)
- If InStr(1, .responseText, "function WSSORedirect()", vbTextCompare) < 1 Then MsgBox "any luck?"
- .Open Method:="GET", URL:=fileURL, async:=True
- .send
- If Not .WaitForResponse(TimeOut:=30000) Then MsgBox "timeout!": GoTo exit_me
- Debug.Print count
- Sleep 2000
- Loop
- Sleep 2000
- fData = .responseBody
- ' Display the results of the request.
- Debug.Print "Credentials: "
- Debug.Print .Status & " " & .StatusText
- Debug.Print .getAllResponseHeaders
- End With
- If Dir(destPath) <> vbNullString Then Kill destPath
- fileNum = FreeFile
- Open destPath For Binary Access Write As #fileNum
- Put #fileNum, 1, fData
- Close #fileNum
- If Dir(destPath2) <> vbNullString Then Kill destPath2
- Set strm = CreateObject("ADODB.Stream")
- With strm
- .Type = 1
- .Open
- .Write winHTTP.responseBody
- .SaveToFile destPath2, 2 'overwrite
- End With
- MsgBox "Completed. Check 'C:Temp'.", vbInformation, "execution completed"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement