Use http.SetCredentials "User", "Pass", 0 for basic server authentication.
Often, you must first send a POST request to a login page, capture the "Set-Cookie" header, and then pass it in your GET request for the file. Step 3: Handling Large Files winhttp winhttprequest 5.1 download file
If you need to download a file from a site that requires credentials, you can use the SetCredentials or SetRequestHeader methods to pass your login information. Use http
' Early Binding: Requires reference to "Microsoft WinHTTP Services, version 5.1" ' and "Microsoft ActiveX Data Objects 6.1 Library" Sub DownloadFile() Dim http As Object Dim stream As Object Dim url As String Dim savePath As String url = "https://example.com" savePath = "C:\Downloads\report.pdf" ' Initialize the WinHTTP object Set http = CreateObject("WinHttp.WinHttpRequest.5.1") ' Open and Send request http.Open "GET", url, False http.Send ' Check if download was successful (Status 200) If http.Status = 200 Then ' Use ADODB.Stream to save binary data Set stream = CreateObject("ADODB.Stream") stream.Open stream.Type = 1 ' 1 = adTypeBinary stream.Write http.ResponseBody stream.SaveToFile savePath, 2 ' 2 = adSaveCreateOverWrite stream.Close MsgBox "Download Complete!" Else MsgBox "Error: " & http.Status & " - " & http.StatusText End If End Sub Use code with caution. Step 2: Downloading from Secure/Login Sites ' Early Binding: Requires reference to "Microsoft WinHTTP
Using the object is one of the most efficient ways to download files in environments like VBA (Excel, Access) and VBScript . Unlike basic methods, WinHTTP provides full control over headers, cookies, and authentication, making it the professional choice for secure or complex downloads. Why Choose WinHttp.WinHttpRequest.5.1?