Solved

Wininet API with basic authorization

Posted on 2003-12-08
8
1,128 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 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
Independent Software Vendors: 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!

 
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

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

697 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