[RESOLVED] Download Image from URL that requires Authentication
Hi,
I need help on downloading images from a URL (that requires username and password) to a directory on my local system.
I have used the following codes :
Private Sub cmdDownload_Click()
Dim sDestPath As String
Dim sFileName As String
Dim sTmp As String
Dim bResult As Boolean
sDestPath = Trim$(txtDestPath.Text)
If sDestPath = vbNullString Then
sDestPath = App.Path
End If
If Right(sDestPath, 1) <> "\" Then
sDestPath = sDestPath & "\"
End If
sDestPath = sDestPath & sFileName
bResult = DownloadFile(txtURL.Text, sDestPath)
If bResult Then
lblResult.Caption = "File downloaded."
Else
lblResult.Caption = "Error on download file."
Exit Sub
End If
If chkViewFile.Value = vbChecked Then
ShellExecute Me.hwnd, "open", sDestPath, vbNullString, vbNullString, 1
End If
End Sub
'This is a bas module
Option Explicit
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Public Function DownloadFile(sURLFile As String, sLocalFilename As String) As Boolean
Dim lRetVal As Long
lRetVal = URLDownloadToFile(0, sURLFile, sLocalFilename, 0, 0)
If lRetVal = 0 Then DownloadFile = True
End Function
But i cant get it done, because the URL requires a username and password, but i dont know how to enter the username and password. I have the username and password, but dont know how to go about using it and make the code work.
Re: Download Image from URL that requires Authentication
I need help @ANYONE.
I need to download image from URL of a website that requires Authentication. I have the username and password but i dont know how to download image using them.
Examples i could find are only for URL without authentication.
Please HELP
Re: Download Image from URL that requires Authentication
You can't use URLDownloadToFile for that if you need authentication. Use the WinINET library instead.
In short, you have to call InternetOpen to initialize it, then InternetConnect which is where you pass the credentials and flags. Then you call HttpOpenRequest to create a request that contains the file to download and pass it to HttpSendRequest. Then InternetReadFile is used to transfer the file. This is somewhat more complicated than just calling URLDownloadToFile but it's very robust. I have VB software written 20 years ago that uses it and is still working.
If you don't mind waiting until tonight I can post some code. I'm at work right now and don't have it handy.
Last edited by Grant Swinger; Jul 14th, 2018 at 12:49 PM.
Reason: Fix typo
Re: Download Image from URL that requires Authentication
Originally Posted by Grant Swinger
You can't use URLDownloadToFile for that if you need authentication. Use the WinINET library instead.
In short, you have to call InternetOpen to initialize it, then InternetConnect which is where you pass the credentials and flags. Then you call HttpOpenRequest to create a request that contains the file to download and pass it to HttpSendRequest. Then InternetReadFile is used to transfer the file. This is somewhat more complicated than just calling URLDownloadToFile but it's very robust. I have VB software written 20 years ago that uses it and is still working.
If you don't mind waiting until tonight I can post some code. I'm at work right now and don't have it handy.
You are a gem and i am so glad on your help. I have been battling this for days and still have gotten no headway.
I look forward to the codes for doing all those processes you have enumerated above.
Re: Download Image from URL that requires Authentication
Please bear with me. I've been working all weekend on a client with "just a minor change" and I'm still sweeping up. I'll try to get this uploaded tomorrow.
Re: Download Image from URL that requires Authentication
You have to properly escape your credentials.
Try this SetCredentials function:
thinBasic Code:
Option Explicit
Private Declare Function WinHttpCrackUrl Lib "winhttp" (ByVal pwszUrl As Long, ByVal dwUrlLength As Long, ByVal dwFlags As Long, lpUrlComponents As Any) As Long
Private Declare Function WinHttpCreateUrl Lib "winhttp" (lpUrlComponents As Any, ByVal dwFlags As Long, ByVal pwszUrl As Long, pdwUrlLength As Long) As Long
Private Type URL_COMPONENTS
dwStructSize As Long
lpszScheme As Long
dwSchemeLength As Long
nScheme As Long
lpszHostName As Long
dwHostNameLength As Long
nPort As Long
lpszUserName As Long
dwUserNameLength As Long
lpszPassword As Long
dwPasswordLength As Long
lpszUrlPath As Long
dwUrlPathLength As Long
lpszExtraInfo As Long
dwExtraInfoLength As Long
End Type
Public Function SetCredentials(sUrl As String, ByVal sUser As String, ByVal sPass As String) As String
Re: Download Image from URL that requires Authentication
Originally Posted by wqweto
You have to properly escape your credentials.
Try this SetCredentials function:
thinBasic Code:
Option Explicit
Private Declare Function WinHttpCrackUrl Lib "winhttp" (ByVal pwszUrl As Long, ByVal dwUrlLength As Long, ByVal dwFlags As Long, lpUrlComponents As Any) As Long
Private Declare Function WinHttpCreateUrl Lib "winhttp" (lpUrlComponents As Any, ByVal dwFlags As Long, ByVal pwszUrl As Long, pdwUrlLength As Long) As Long
Private Type URL_COMPONENTS
dwStructSize As Long
lpszScheme As Long
dwSchemeLength As Long
nScheme As Long
lpszHostName As Long
dwHostNameLength As Long
nPort As Long
lpszUserName As Long
dwUserNameLength As Long
lpszPassword As Long
dwPasswordLength As Long
lpszUrlPath As Long
dwUrlPathLength As Long
lpszExtraInfo As Long
dwExtraInfoLength As Long
End Type
Public Function SetCredentials(sUrl As String, ByVal sUser As String, ByVal sPass As String) As String
I could not get to use this code. I replaced bResult = DownloadFile(SetCredentials(txtURL.Text, "solu_v", "Mk66'5GDPm?Z9+&/"), sDestPath) but i cant seem to get the function "SetCredentials" as it should be. Can you pls give a complete code ?
Re: Download Image from URL that requires Authentication
Originally Posted by Tobyy
I could not get to use this code. I replaced bResult = DownloadFile(SetCredentials(txtURL.Text, "solu_v", "Mk66'5GDPm?Z9+&/"), sDestPath) but i cant seem to get the function "SetCredentials" as it should be. Can you pls give a complete code ?
Copy the code snippet from Option Explicit to End Function of pvEscapeCredential *verbatim* into a *separate* standard .bas module.
Just tested this on Win10 in VBIDE in the default form
Re: Download Image from URL that requires Authentication
Here you go. I added a small form that calls the BAS module. The code is old but it works. You can step through it with the debugger easily to see how it works. Reading the MSDN documentation on the functions called while you do is recommended.
Please note:
For the web server name just use the domain name (mywebserver.com). Don't use prefixes like www or http:/
The file path on the server must have leading and trailing slashes. Like this: /pathtomyfiles/
Re: Download Image from URL that requires Authentication
Originally Posted by Grant Swinger
Here you go. I added a small form that calls the BAS module. The code is old but it works. You can step through it with the debugger easily to see how it works. Reading the MSDN documentation on the functions called while you do is recommended.
Please note:
For the web server name just use the domain name (mywebserver.com). Don't use prefixes like www or http:/
The file path on the server must have leading and trailing slashes. Like this: /pathtomyfiles/
Have fun!
Had difficulties using the program. It really didn't get through for me.
Re: Download Image from URL that requires Authentication
I replaced bResult = DownloadFile(SetCredentials(txtURL.Text, "solu_v", "Mk66'5GDPm?Z9+&/"), sDestPath)
should we assume that you replaced the literal username and password with you own?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Re: Download Image from URL that requires Authentication
Doesn't seem to be much to this. What am I missing? Just use the ActiveX API instead of the flat API.
Code:
Option Explicit
'
'Reference to: Microsoft WinHTTP Services, version 5.1
'
'This is missing from the type library info:
Private Enum HTTPREQUEST_SETCREDENTIALS_FLAGS
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = &H0
HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = &H1
End Enum
Private Sub cmdFetch_Click()
Dim Req As WinHttp.WinHttpRequest
Dim F As Integer
Enabled = False
Set Req = New WinHttp.WinHttpRequest
With Req
.Option(WinHttpRequestOption_SecureProtocols) = SecureProtocol_TLS1_2
.SetAutoLogonPolicy AutoLogonPolicy_Never
.SetTimeouts 5000, 5000, 5000, 5000
.Open "GET", txtURL.Text, Async:=False
.SetCredentials txtUser.Text, _
txtPW.Text, _
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
.Send
If .Status <> 200 Then
MsgBox "Bad status " & CStr(.Status) _
& vbNewLine & vbNewLine _
& .StatusText
Else
On Error Resume Next
Kill txtFile.Text
On Error GoTo 0
F = FreeFile(0)
Open txtFile.Text For Binary Access Write As #F
Put #F, , .ResponseBody
Close #F
MsgBox "Complete"
End If
End With
Enabled = True
txtURL.SetFocus
End Sub
You can also use async requests. That allows you to report progress and not have your UI freeze.
There is no magic to URLDownloadToFile() you know. It always needs enough memory to hold the entire downloaded resource before writing it to disk.