?
Solved

Wininet API with basic authorization

Posted on 2003-12-08
8
Medium Priority
?
1,150 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 498 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 501 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 32

Accepted Solution

by:
jadedata earned 501 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

850 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