Download files from secure Internet site (HTTPS, SSL) using WinInet API 
Author Message
 Download files from secure Internet site (HTTPS, SSL) using WinInet API

I'm trying to download a file from a secure (HTTPS) location. The site
has a login form. I can access the site through the IE browser by
specifying the server and the login form (i.e
https://www.tbmgkw.com/login/). Then username and password are
prompted.  I would like to be able to download files programmatically
through VB.  But I'm not having any lucky with this.  Here is the code
I'm using. The HttpSendRequest function is returning false. Also,
strangely, the InternetConnect always returns a valid connection hadle
even if I specify a servername that doesn't exist.

I would appreciate any help.
Shabeel

Option Explicit
Private Declare Function InternetOpen Lib "wininet.dll" Alias
"InternetOpenA" ( _
    ByVal lpszAgent As String, _
    ByVal dwAccessType As Long, _
    ByVal lpszProxyName As String, _
    ByVal lpszProxyBypass As String, _
    ByVal dwFlags As Long) As Long
Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Declare Function InternetConnect Lib "wininet.dll" Alias
"InternetConnectA" ( _
    ByVal hInternetSession As Long, _
    ByVal lpszServerName As String, _
    ByVal nServerPort As Integer, _
    ByVal lpszUsername As String, _
    ByVal lpszPassword As String, _
    ByVal dwService As Long, _
    ByVal dwFlags As Long, _
    ByVal dwContext As Long) As Long
Const INTERNET_SERVICE_HTTP = 3
Private Declare Function HttpOpenRequest Lib "wininet.dll" Alias
"HttpOpenRequestA" ( _
    ByVal hHttpSession As Long, _
    ByVal lpszVerb As String, _
    ByVal lpszObjectName As String, _
    ByVal lpszVersion As String, _
    ByVal lpszReferer As String, _
    ByVal lpszAcceptTypes As String, _
    ByVal dwFlags As Long, _
    ByVal dwContext As Long) As Long

Const INTERNET_FLAG_NO_COOKIES = &H80000
Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
Private Declare Function HttpSendRequest Lib "wininet.dll" Alias
"HttpSendRequestA" ( _
    ByVal hHttpRequest As Long, _
    ByVal lpszHeaders As String, _
    ByVal dwHeadersLength As Long, _
    ByVal lpOptional As String, _
    ByVal dwOptionalLength As Long) As Boolean

Private Declare Function InternetReadFile Lib "wininet.dll" ( _
    ByVal hFile As Long, _
    ByVal lpBuffer As String, _
    ByVal dwNumberOfBytesToRead As Long, _
    ByRef lpNumberOfBytesRead As Long) As Boolean

Private Sub Form_Load()
    Dim hInternet As Long
    hInternet = InternetOpen(App.Title, INTERNET_OPEN_TYPE_DIRECT,
vbNullString, vbNullString, 0)

    Dim hConnect As Long
   ' hConnect = InternetConnect(hInternet, strServer, iPort, "", "",
INTERNET_SERVICE_HTTP, 0, 0)
   hConnect = InternetConnect(hInternet,
"https://www.tbmgkw.com/HHANKS/", 443, "USERNAME", "PASSWORD",
INTERNET_SERVICE_HTTP, 0, 0)

    Dim lFlags As Long
    Dim hRequest As Long
    Dim strURL As String
    lFlags = INTERNET_FLAG_NO_COOKIES
    lFlags = lFlags Or INTERNET_FLAG_NO_CACHE_WRITE
strURL = "GetMeThisFile.TXT"
    hRequest = HttpOpenRequest(hConnect, "GET", strURL, "HTTP/1.0",
vbNullString, vbNullString, lFlags, 0)

    Dim bRes As Boolean

    bRes = HttpSendRequest(hRequest, vbNullString, 0, vbNullString, 0)

    Dim strBuffer As String * 1
    Dim strFile As String
    Dim strDir  As String
    'Dim strBuffer As String * 1024
    Dim lBytesRead As Long
    strFile = "newdownload.txt"
    strDir = Dir(App.Path & "\" & strFile)

    If Len(strDir) > 0 Then
    Kill App.Path & "\" & strFile
End If
Dim ifile As Integer
ifile = FreeFile()
Open App.Path & "\" & strFile For Binary Access Write As ifile

Do
    bRes = InternetReadFile(hRequest, strBuffer, Len(strBuffer),
lBytesRead)
    If lBytesRead > 0 Then
        Put ifile, , strBuffer
    End If
Loop While lBytesRead > 0

End Sub



Sun, 04 Sep 2005 01:30:31 GMT  
 Download files from secure Internet site (HTTPS, SSL) using WinInet API

Quote:

> I'm trying to download a file from a secure (HTTPS) location. The site
> has a login form. I can access the site through the IE browser by
> specifying the server and the login form (i.e
> https://www.tbmgkw.com/login/). Then username and password are
> prompted.  I would like to be able to download files programmatically
> through VB.  But I'm not having any lucky with this.  Here is the code
> I'm using. The HttpSendRequest function is returning false. Also,
> strangely, the InternetConnect always returns a valid connection hadle
> even if I specify a servername that doesn't exist.

If the server uses forms to login, your approach won't work. You need to
access the URL that is specified in login form first, get the cookie,
and after that send the cookie to the server when requesting the file
for download.
This is how TimelyWeb (http://www.timelyweb.com/) works.

--
Eugene Mayevski,
SSL/TLS support for your VB or C++ application
http://www.secureblackbox.com/activex-ssl.html



Mon, 05 Sep 2005 01:49:33 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Download files from secure Internet site (HTTPS, SSL) using WinInet API

2. Retrieving SSL-secured pages over the HTTPS-Protocol using the INet-Control

3. Using WinINet API to download a file (w/o any OCXes)

4. Using WinINet API to download a file (w/o any OCXes)

5. Help needed POSTing to SSL server using wininet.dll API calls

6. Active-X control for downloading file from SSL site

7. Read Certificate Info for SSL using WinInet API, 12018 error

8. https connection using WinInet API in VB5.0 freezes on exit

9. Internet Transfer Control and https (SSL) - win98 vs win95

10. Internet Transfer Control and https (SSL) - win98 vs win95

11. Internet Transfer Control and https (SSL)

12. Accessing a Secure site (SSL 3.0) with VB??

 

 
Powered by phpBB® Forum Software