Solved

Wininet API with basic authorization

Posted on 2003-12-08
8
1,121 Views
Last Modified: 2008-02-01
I'm trying to send a request to a website to automatically download a file.  My url string works but the website prompts for a username and password.  I want to automate this as well so I guess I'm looking for a way (possibly Wininet API call) to enter the username and password automatically.
0
Comment
Question by:jbpowell
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 23

Assisted Solution

by:heer2351
heer2351 earned 166 total points
ID: 9899481
You can specify a username and a password in a URL:

ftp://username:password@hostname/directory/file
0
 
LVL 5

Assisted Solution

by:Atropa
Atropa earned 167 total points
ID: 9899503
You just need to add the username and password to your ftp string..

ftp://USERNAME:PASSWORD@ftp.YourFTPSite.com/

That should do you up golden.  /Atropa
0
 

Author Comment

by:jbpowell
ID: 9899506
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 5

Expert Comment

by:Atropa
ID: 9899518
Damn, you beat me to the punch as usual heer
0
 

Author Comment

by:jbpowell
ID: 9899557
I tried it with the https:// above and could not get it to work.

The documentation I have from the website mentions an "authentication header".  I'm pretty good with Access and  have some limited vb skills, but when it comes to internet apps I'm obviously clueless.
0
 
LVL 23

Expert Comment

by:heer2351
ID: 9899567
0
 
LVL 32

Accepted Solution

by:
jadedata earned 167 total points
ID: 9899577
Hey jbpowell!

  http://www.mvps.org/access/modules/mdl0037.htm
  Internet Transfer Library contains both HTTP and FTP processes that work quite nicely.
  The library is a .mda or you can cut/paste/run the code as a Class Module in the application.

regards
Jack
0
 

Author Comment

by:jbpowell
ID: 9900095
I ended up modifying the following code to include the v_auth and v_auth_len variables.

I had been using this for a webserver that did not require authentication the v_auth variable was hardcoded to "" in that case.

Also, I had to encode the username:password in base64 for the www.steelroads.com website in order to get it to work.

Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INTERNET_OPEN_TYPE_PROXY = 3
Private Const INTERNET_INVALID_PORT_NUMBER = 0
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000

Public Function GetInternetFile(v_sURL As String, v_sFileName As String) As Boolean
'***********************************************************************************************
' Subroutine: GetInternetFile
' Parameters:   v_sURL - full URL address
'               sFileNaname - file path
'
' Will download a file from the provided URL and store the response as binary data
' into the specified file.
'
'***********************************************************************************************
On Error GoTo error_GetInternetFile
    Dim hFile As Long
    Dim hSession As Long
    Dim v_auth as String
    Dim v_auth_len as Long
   
'get connection to internet
    hSession = InternetOpen("", INTERNET_OPEN_TYPE_PRECONFIG, "", "", 0)
    If hSession = 0 Then
        If err.LastDllError > 12000 And err.LastDllError < 12200 Then
            MsgBox "Couldn't open internet connection because: "
        Else
            MsgBox "Couldn't open internet connection"
        End If
        Exit Function
    End If

' get file where v_auth is the authorization header and v_auth_len is the length of that string
    hFile = InternetOpenURL(hSession, v_sURL, v_auth, v_auth_len, INTERNET_FLAG_RELOAD, 0)
    If hFile = 0 Then
        GoTo error_GetInternetFile
        If err.LastDllError > 12000 And err.LastDllError < 12200 Then
            MsgBox "Couldn't open web page because: "
        Else
            MsgBox "Couldn't open web page"
        End If
    Else
        'connection to web page obtained, download contents
       
       
        Dim lDownloadSize As Long, lFileSize As Long
        Dim sBuffer As String * 1024, iFile As Integer
        iFile = FreeFile
        lDownloadSize = 1
       
        'get server version of product
       
        Open v_sFileName For Binary Access Write As #1
'        UpdateStatus 103
        Do Until lDownloadSize = 0
            If InternetReadFile(hFile, sBuffer, Len(sBuffer), lDownloadSize) <> 0 Then
                Put #1, lFileSize + 1, sBuffer
                lFileSize = lFileSize + lDownloadSize
                sBuffer = Space(1024)
            End If
        Loop
        Close #1
        InternetCloseHandle hFile
    End If
GetInternetFile = True

'Function completed without error
exit_GetInternetFile:
    InternetCloseHandle hFile
    Exit Function

'Handle Error
error_GetInternetFile:
    GetInternetFile = False


    GoTo exit_GetInternetFile
   
End Function
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question