Search code examples
apiwinapivbavb6wininet

Download URL Contents Directly into String (VB6) WITHOUT Saving to Disk


Basically, I want to download the contents of a particular URL (basically, just HTML codes in the form of a String) into my VB6 String variable. However, there are some conditions.

I know about the URLDownloadToFile Function - however, this requires that you save the downloaded file/HTML onto a file location on disk before you can read it into a String variable, this is not an option for me and I do not want to do this.

The other thing is, if I need to use an external library, it must already come with all versions of Windows from XP and onwards, I cannot use a control or library that I am required to ship, package and distribute even if it is free, this is not an option and I do not want to do this. So, I cannot use the MSINET.OCX (Internet Transfer) Control's .OpenURL() function (which simply returns contents into a String), as it does not come with Windows.

Is there a way to be able to do this with the Windows API, URLMON or something else that is pre-loaded into or comes with Windows, or a way to do it in VB6 (SP6) entirely?

If so, I would appreciate direction, because even after one hour of googling, the only examples I've found are references to URLDownloadToFile (which requires saving on disk before being ale to place into a String) and MsInet.OpenURL (which requires that I ship and distribute MSINET.OCX, which I cannot and don't want to do).

Surely there has got to be an elegant way to be able to do this? I can do it in VB.NET without an issue, but obviously don't have the luxury of the .NET framework in VB6 - any ideas?

Update:

I have found this: http://www.freevbcode.com/ShowCode.asp?ID=1252 however it says that the displayed function may not return the entire page and links to a Microsoft bug report or kb article explaining this. Also, I understand this is based off wininet.dll - and I'm wondering which versions of Windows does WinInet.dll come packaged with? Windows XP & beyond? Does it come with Windows 7 and/or Windows 8?


Solution

  • If you want to do this with pure VB, and no IE, then you can take advantage of a little-used features of the VB UserControl - async properties.

    Create a new UserControl, and call it something like UrlDownloader. Set the InvisibleAtRuntime property to True. Add the following code to it:

    Option Explicit
    
    Private Const m_ksProp_Data         As String = "Data"
    
    Private m_bAsync                    As Boolean
    Private m_sURL                      As String
    
    Public Event AsyncReadProgress(ByRef the_abytData() As Byte)
    Public Event AsyncReadComplete(ByRef the_abytData() As Byte)
    
    Public Property Let Async(ByVal the_bValue As Boolean)
        m_bAsync = the_bValue
    End Property
    
    Public Property Get Async() As Boolean
        Async = m_bAsync
    End Property
    
    Public Property Let URL(ByVal the_sValue As String)
        m_sURL = the_sValue
    End Property
    
    Public Property Get URL() As String
        URL = m_sURL
    End Property
    
    Public Sub Download()
    
        UserControl.AsyncRead m_sURL, vbAsyncTypeByteArray, m_ksProp_Data, IIf(m_bAsync, 0&, vbAsyncReadSynchronousDownload)
    
    End Sub
    
    Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
    
        If AsyncProp.PropertyName = m_ksProp_Data Then
            RaiseEvent AsyncReadComplete(AsyncProp.Value)
        End If
    
    End Sub
    
    Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)
    
        If AsyncProp.PropertyName = m_ksProp_Data Then
            Select Case AsyncProp.StatusCode
            Case vbAsyncStatusCodeBeginDownloadData, vbAsyncStatusCodeDownloadingData, vbAsyncStatusCodeEndDownloadData
                RaiseEvent AsyncReadProgress(AsyncProp.Value)
            End Select
        End If
    
    End Sub
    

    To use this control, stick it on a form and use the following code:

    Option Explicit
    
    Private Sub Command1_Click()
    
        XDownload1.Async = False
        XDownload1.URL = "http://www.google.co.uk"
        XDownload1.Download
    
    End Sub
    
    Private Sub XDownload1_AsyncReadProgress(the_abytData() As Byte)
    
        Debug.Print StrConv(the_abytData(), vbUnicode)
    
    End Sub
    

    Suffice to say, you can customise this to your hearts content. It can tell (using the AyncProp object) whether the file is cached, and other useful information. It even has a special mode in which you can download GIF, JPG and BMP files and return them as a StdPicture object!