Solved

Wininet API with basic authorization

Posted on 2003-12-08
8
1,112 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 5

Expert Comment

by:Atropa
Comment Utility
Damn, you beat me to the punch as usual heer
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jbpowell
Comment Utility
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
Comment Utility
0
 
LVL 32

Accepted Solution

by:
jadedata earned 167 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now