Results 1 to 15 of 15

Thread: [RESOLVED] Download Image from URL that requires Authentication

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2018
    Posts
    56

    Resolved [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

    sFileName = Mid$(txtURL.Text, InStrRev(txtURL.Text, "/") + 1)

    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.

    Please help.

  2. #2

    Thread Starter
    Member
    Join Date
    Feb 2018
    Posts
    56

    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

  3. #3
    New Member
    Join Date
    Jul 2014
    Posts
    3

    Re: Download Image from URL that requires Authentication

    Add the credentials to the URL in this way:

    Code:
    "http://username:password@www.domain.com/subfolder/filename.ext"
    webgaldom

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2018
    Posts
    56

    Re: Download Image from URL that requires Authentication

    Quote Originally Posted by webgaldom View Post
    Add the credentials to the URL in this way:

    Code:
    "http://username:password@www.domain.com/subfolder/filename.ext"
    webgaldom
    Thanks so much for the help. I did that and still got an error of file download.

    Attached is the code being used.

    The username of the website is solu_v and the password is Mk66'5GDPm?Z9+&/

    So the url according to your help looked like this :

    http://solu_v:Mk66&#39;5GDPm?Z9+&/@52.18...1321140281.jpg

    but still did not work with the attached code.

    Pls help
    Attached Files Attached Files

  5. #5
    Lively Member Grant Swinger's Avatar
    Join Date
    Jul 2015
    Posts
    71

    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

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2018
    Posts
    56

    Re: Download Image from URL that requires Authentication

    Quote Originally Posted by Grant Swinger View Post
    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.

    Thanks so much.

  7. #7
    Lively Member Grant Swinger's Avatar
    Join Date
    Jul 2015
    Posts
    71

    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.

  8. #8
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Download Image from URL that requires Authentication

    You have to properly escape your credentials.

    Try this SetCredentials function:
    thinBasic Code:
    1. Option Explicit
    2.  
    3. Private Declare Function WinHttpCrackUrl Lib "winhttp" (ByVal pwszUrl As Long, ByVal dwUrlLength As Long, ByVal dwFlags As Long, lpUrlComponents As Any) As Long
    4. Private Declare Function WinHttpCreateUrl Lib "winhttp" (lpUrlComponents As Any, ByVal dwFlags As Long, ByVal pwszUrl As Long, pdwUrlLength As Long) As Long
    5.  
    6. Private Type URL_COMPONENTS
    7.     dwStructSize        As Long
    8.     lpszScheme          As Long
    9.     dwSchemeLength      As Long
    10.     nScheme             As Long
    11.     lpszHostName        As Long
    12.     dwHostNameLength    As Long
    13.     nPort               As Long
    14.     lpszUserName        As Long
    15.     dwUserNameLength    As Long
    16.     lpszPassword        As Long
    17.     dwPasswordLength    As Long
    18.     lpszUrlPath         As Long
    19.     dwUrlPathLength     As Long
    20.     lpszExtraInfo       As Long
    21.     dwExtraInfoLength   As Long
    22. End Type
    23.  
    24. Public Function SetCredentials(sUrl As String, ByVal sUser As String, ByVal sPass As String) As String
    25.     Dim uUrlCom         As URL_COMPONENTS
    26.     Dim sRet            As String
    27.     Dim lSize           As Long
    28.  
    29.     With uUrlCom
    30.         .dwStructSize = Len(uUrlCom)
    31.         .dwSchemeLength = -1
    32.         .dwHostNameLength = -1
    33.         .dwUrlPathLength = -1
    34.         .dwExtraInfoLength = -1
    35.     End With
    36.     Call WinHttpCrackUrl(StrPtr(sUrl), Len(sUrl), 0, uUrlCom)
    37.     sUser = pvEscapeCredential(sUser)
    38.     sPass = pvEscapeCredential(sPass)
    39.     With uUrlCom
    40.         .lpszUserName = StrPtr(sUser)
    41.         .dwUserNameLength = Len(sUser)
    42.         .lpszPassword = StrPtr(sPass)
    43.         .dwPasswordLength = Len(sPass)
    44.     End With
    45.     sRet = String(4096, 0)
    46.     lSize = Len(sRet)
    47.     Call WinHttpCreateUrl(uUrlCom, 0, StrPtr(sRet), lSize)
    48.     SetCredentials = Left$(sRet, InStr(sRet, vbNullChar))
    49. End Function
    50.  
    51. Private Function pvEscapeCredential(sText As String) As String
    52.     pvEscapeCredential = Replace(Replace(Replace(Replace(sText, _
    53.         "%", "%" & Hex(Asc("%"))), _
    54.         "/", "%" & Hex(Asc("/"))), _
    55.         "@", "%" & Hex(Asc("@"))), _
    56.         "?", "%" & Hex(Asc("?")))
    57. End Function
    The idea is in cmdDownload_Click to replace this

    | bResult = DownloadFile(txtURL.Text, sDestPath)

    with this

    | bResult = DownloadFile(SetCredentials(txtURL.Text, "solu_v", "Mk66'5GDPm?Z9+&/"), sDestPath)

    cheers,
    </wqw>

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2018
    Posts
    56

    Re: Download Image from URL that requires Authentication

    Quote Originally Posted by wqweto View Post
    You have to properly escape your credentials.

    Try this SetCredentials function:
    thinBasic Code:
    1. Option Explicit
    2.  
    3. Private Declare Function WinHttpCrackUrl Lib "winhttp" (ByVal pwszUrl As Long, ByVal dwUrlLength As Long, ByVal dwFlags As Long, lpUrlComponents As Any) As Long
    4. Private Declare Function WinHttpCreateUrl Lib "winhttp" (lpUrlComponents As Any, ByVal dwFlags As Long, ByVal pwszUrl As Long, pdwUrlLength As Long) As Long
    5.  
    6. Private Type URL_COMPONENTS
    7.     dwStructSize        As Long
    8.     lpszScheme          As Long
    9.     dwSchemeLength      As Long
    10.     nScheme             As Long
    11.     lpszHostName        As Long
    12.     dwHostNameLength    As Long
    13.     nPort               As Long
    14.     lpszUserName        As Long
    15.     dwUserNameLength    As Long
    16.     lpszPassword        As Long
    17.     dwPasswordLength    As Long
    18.     lpszUrlPath         As Long
    19.     dwUrlPathLength     As Long
    20.     lpszExtraInfo       As Long
    21.     dwExtraInfoLength   As Long
    22. End Type
    23.  
    24. Public Function SetCredentials(sUrl As String, ByVal sUser As String, ByVal sPass As String) As String
    25.     Dim uUrlCom         As URL_COMPONENTS
    26.     Dim sRet            As String
    27.     Dim lSize           As Long
    28.  
    29.     With uUrlCom
    30.         .dwStructSize = Len(uUrlCom)
    31.         .dwSchemeLength = -1
    32.         .dwHostNameLength = -1
    33.         .dwUrlPathLength = -1
    34.         .dwExtraInfoLength = -1
    35.     End With
    36.     Call WinHttpCrackUrl(StrPtr(sUrl), Len(sUrl), 0, uUrlCom)
    37.     sUser = pvEscapeCredential(sUser)
    38.     sPass = pvEscapeCredential(sPass)
    39.     With uUrlCom
    40.         .lpszUserName = StrPtr(sUser)
    41.         .dwUserNameLength = Len(sUser)
    42.         .lpszPassword = StrPtr(sPass)
    43.         .dwPasswordLength = Len(sPass)
    44.     End With
    45.     sRet = String(4096, 0)
    46.     lSize = Len(sRet)
    47.     Call WinHttpCreateUrl(uUrlCom, 0, StrPtr(sRet), lSize)
    48.     SetCredentials = Left$(sRet, InStr(sRet, vbNullChar))
    49. End Function
    50.  
    51. Private Function pvEscapeCredential(sText As String) As String
    52.     pvEscapeCredential = Replace(Replace(Replace(Replace(sText, _
    53.         "%", "%" & Hex(Asc("%"))), _
    54.         "/", "%" & Hex(Asc("/"))), _
    55.         "@", "%" & Hex(Asc("@"))), _
    56.         "?", "%" & Hex(Asc("?")))
    57. End Function
    The idea is in cmdDownload_Click to replace this

    | bResult = DownloadFile(txtURL.Text, sDestPath)

    with this

    | bResult = DownloadFile(SetCredentials(txtURL.Text, "solu_v", "Mk66'5GDPm?Z9+&/"), sDestPath)

    cheers,
    </wqw>
    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 ?

  10. #10
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Download Image from URL that requires Authentication

    Quote Originally Posted by Tobyy View Post
    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
    thinBasic Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Debug.Print SetCredentials("http://www.google.com", "solu_v", "Mk66'5GDPm?Z9+&/")
    5. End Sub
    Prints this in Immediate Window
    Code:
    http://solu_v:Mk66'5GDPm%3FZ9+&%2F@www.google.com/
    cheers,
    </wqw>

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2018
    Posts
    56

    Re: Download Image from URL that requires Authentication

    Quote Originally Posted by wqweto View Post
    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
    thinBasic Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Debug.Print SetCredentials("http://www.google.com", "solu_v", "Mk66'5GDPm?Z9+&/")
    5. End Sub
    Prints this in Immediate Window
    Code:
    http://solu_v:Mk66'5GDPm%3FZ9+&%2F@www.google.com/
    cheers,
    </wqw>
    It worked like a Charm !!!!! Thanks so so so much.

  12. #12
    Lively Member Grant Swinger's Avatar
    Join Date
    Jul 2015
    Posts
    71

    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/

    Have fun!
    Attached Files Attached Files

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2018
    Posts
    56

    Re: Download Image from URL that requires Authentication

    Quote Originally Posted by Grant Swinger View Post
    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.

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width