Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Wininet API with basic authorization

Posted on 2003-12-08
8
Medium Priority
?
1,146 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 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