PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Download Image from URL that requires Authentication-VBForums
Results 1 to 10 of 10
  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    8

    Thumbs up 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
    New Member
    Join Date
    Feb 2018
    Posts
    8

    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
    New Member
    Join Date
    Feb 2018
    Posts
    8

    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
    Junior Member Grant Swinger's Avatar
    Join Date
    Jul 2015
    Posts
    29

    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
    New Member
    Join Date
    Feb 2018
    Posts
    8

    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
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    628

    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>

  8. #8
    Junior Member Grant Swinger's Avatar
    Join Date
    Jul 2015
    Posts
    29

    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.

  9. #9
    Junior Member Grant Swinger's Avatar
    Join Date
    Jul 2015
    Posts
    29

    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

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    8

    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 ?

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width