Downloading Binary HTTP Data

Let's say that we want to download a file named image.jpg at http://www.server.com/museums. We can do that with the Internet Transfer control, as you might expect. We fill a binary array with the data from the binary file and pass the icByteArray parameter to OpenURL() to indicate that we are downloading binary data:

Private Sub GetFile_Click()
—> Dim binarydata() As Byte
    RichTextBox1.Text =
        Inet1.OpenURL("http://www.microsoft.com/vbasic/default.htm")
—> binarydata() =
        Inet1.OpenURL("http://www.server.com/museums/image.jpg", icByteArray)
        .
        .
        .
End Sub

Now that the file has been loaded into the binary array, we store the file on disk:

Private Sub GetFile_Click()
    Dim binarydata() As Byte
    RichTextBox1.Text =
        Inet1.OpenURL("http://www.microsoft.com/vbasic/default.htm")
    binarydata() =
        Inet1.OpenURL("http://www.server.com/museums/image.jpg", icByteArray)
—> Open "E:\vb\image.jpg" For Binary Access Write As #1
—> Put #1, , binarydata()
—> Close #1
     .
     .
     .
End Sub

Finally, let's put a message box on the screen indicating that we have finished our downloading operations. (This code would be placed in the Internet Transfer control's StateChanged event handler if you were using the asynchronous Execute method.)

Private Sub GetFile_Click()
    Dim binarydata() As Byte
    RichTextBox1.Text =
        Inet1.OpenURL("http://www.microsoft.com/vbasic/default.htm")
    binarydata() =
        Inet1.OpenURL("http://www.server.com/museums/image.jpg", icByteArray)
    Open "E:\vb\image.jpg" For Binary Access Write As #1
    Put #1, , binarydata()
    Close #1
—> MsgBox "Finished."
End Sub

Now we can download binary files as well as text files. Using the Internet Transfer control, the process is easy: we just use OpenURL() or the Execute() method, and Visual Basic handles the rest. The code for the HTTPer project appears in Listing 3.2.

Listing 3.2 (HTTPer) FrmMain.frm

VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.1#0"; "COMDLG32.OCX"
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.1#0"; "COMCTL32.OCX"
Object = "{48E59290-9880-11CF-9754-00AA00C00908}#1.0#0"; "MSINET.OCX"
Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.1#0"; "RICHTX32.OCX"
Begin VB.Form frmMain
   Caption         =   "HTTPer"
   ClientHeight    =   4050
   ClientLeft      =   165
   ClientTop       =   735
   ClientWidth     =   6315
   LinkTopic       =   "Form1"
   ScaleHeight     =   4050
   ScaleWidth      =   6315
   StartUpPosition =   3  'Windows Default
   Begin ComctlLib.Toolbar tbToolBar
      Align           =   1  'Align Top
      Height          =   420
      Left            =   0
      TabIndex        =   1
      Top             =   0
      Width           =   6315
      _ExtentX        =   11139
      _ExtentY        =   741
      ButtonWidth     =   635
      ButtonHeight    =   582
      Appearance      =   1
      ImageList       =   "imlIcons"
      BeginProperty Buttons {7791BA41-E020-11CF-8E74-00A0C90F26F8}
         NumButtons      =   17
         BeginProperty Button1 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "New"
            Object.ToolTipText     =   "New"
            Object.Tag             =   ""
            ImageIndex      =   1
         EndProperty
         BeginProperty Button2 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Open"
            Object.ToolTipText     =   "Open"
            Object.Tag             =   ""
            ImageIndex      =   2
         EndProperty
         BeginProperty Button3 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Save"
            Object.ToolTipText     =   "Save"
            Object.Tag             =   ""
            ImageIndex      =   3
         EndProperty
         BeginProperty Button4 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Object.Tag             =   ""
            Style           =   3
         EndProperty
         BeginProperty Button5 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Print"
            Object.ToolTipText     =   "Print"
            Object.Tag             =   ""
            ImageIndex      =   4
         EndProperty
         BeginProperty Button6 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Object.Tag             =   ""
            Style           =   3
         EndProperty
         BeginProperty Button7 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Cut"
            Object.ToolTipText     =   "Cut"
            Object.Tag             =   ""
            ImageIndex      =   5
         EndProperty
         BeginProperty Button8 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Copy"
            Object.ToolTipText     =   "Copy"
            Object.Tag             =   ""
            ImageIndex      =   6
         EndProperty
         BeginProperty Button9 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Paste"
            Object.ToolTipText     =   "Paste"
            Object.Tag             =   ""
            ImageIndex      =   7
         EndProperty
         BeginProperty Button10 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Object.Tag             =   ""
            Style           =   3
         EndProperty
         BeginProperty Button11 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Bold"
            Object.ToolTipText     =   "Bold"
            Object.Tag             =   ""
            ImageIndex      =   8

EndProperty

BeginProperty Button12 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Italic"
            Object.ToolTipText     =   "Italic"
            Object.Tag             =   ""
            ImageIndex      =   9
         EndProperty
         BeginProperty Button13 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Underline"
            Object.ToolTipText     =   "Underline"
            Object.Tag             =   ""
            ImageIndex      =   10
         EndProperty
         BeginProperty Button14 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Object.Tag             =   ""
            Style           =   3
         EndProperty
         BeginProperty Button15 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Left"
            Object.ToolTipText     =   "Left Justify"
            Object.Tag             =   ""
            ImageIndex      =   11
         EndProperty
         BeginProperty Button16 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Center"
            Object.ToolTipText     =   "Center"
            Object.Tag             =   ""
            ImageIndex      =   12
         EndProperty
         BeginProperty Button17 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
            Key             =   "Right"
            Object.ToolTipText     =   "Right Justify"
            Object.Tag             =   ""
            ImageIndex      =   13
         EndProperty
      EndProperty
   End
   Begin ComctlLib.StatusBar sbStatusBar
      Align           =   2  'Align Bottom
      Height          =   270
      Left            =   0
      TabIndex        =   0
      Top             =   3780
      Width           =   6315
      _ExtentX        =   11139
      _ExtentY        =   476
      SimpleText      =   ""
      BeginProperty Panels {2C787A51-E01C-11CF-8E74-00A0C90F26F8}
         NumPanels       =   3
         BeginProperty Panel1 {2C787A53-E01C-11CF-8E74-00A0C90F26F8}
            AutoSize        =   1
            Object.Width           =   5503
            MinWidth        =   2540
            Text            =   "Status"
            TextSave        =   "Status"
            Object.Tag             =   ""
         EndProperty
         BeginProperty Panel2 {2C787A53-E01C-11CF-8E74-00A0C90F26F8}
            Style           =   6
            AutoSize        =   2
            Object.Width           =   2540
            MinWidth        =   2540
            TextSave        =   "12/4/96"
            Object.Tag             =   ""
         EndProperty
         BeginProperty Panel3 {2C787A53-E01C-11CF-8E74-00A0C90F26F8}
            Style           =   5
            AutoSize        =   2
            Object.Width           =   2540
            MinWidth        =   2540
            TextSave        =   "9:25 PM"
            Object.Tag             =   ""
         EndProperty
      EndProperty
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
   End
   Begin MSComDlg.CommonDialog dlgCommonDialog
      Left            =   1740
      Top             =   1350
      _ExtentX        =   847
      _ExtentY        =   847
      FontSize        =   1.87933e-37
   End
   Begin InetCtlsObjects.Inet Inet1
      Left            =   2040
      Top             =   1320
      _ExtentX        =   1005
      _ExtentY        =   1005
   End

Begin RichTextLib.RichTextBox RichTextBox1

Height          =   3135
      Left            =   120
      TabIndex        =   2
      Top             =   600
      Width           =   6015
      _ExtentX        =   10610
      _ExtentY        =   5530
      ScrollBars      =   3
      TextRTF         =   $"frmMain.frx":0000
   End
   Begin ComctlLib.ImageList imlIcons
      Left            =   1740
      Top             =   1350
      _ExtentX        =   1005
      _ExtentY        =   1005
      BackColor       =   -2147483643
      ImageWidth      =   16
      ImageHeight     =   16
      MaskColor       =   12632256
      BeginProperty Images {8556BCD1-E01E-11CF-8E74-00A0C90F26F8}
         NumListImages   =   13
         BeginProperty ListImage1 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":00D2
            Key             =   ""
         EndProperty
         BeginProperty ListImage2 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":0424
            Key             =   ""
         EndProperty
         BeginProperty ListImage3 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":0776
            Key             =   ""
         EndProperty
         BeginProperty ListImage4 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":0AC8
            Key             =   ""
         EndProperty
         BeginProperty ListImage5 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":0E1A
            Key             =   ""
         EndProperty
         BeginProperty ListImage6 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":116C
            Key             =   ""
         EndProperty
         BeginProperty ListImage7 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":14BE
            Key             =   ""
         EndProperty
         BeginProperty ListImage8 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":1810
            Key             =   ""
         EndProperty
         BeginProperty ListImage9 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":1B62
            Key             =   ""
         EndProperty
         BeginProperty ListImage10 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":1EB4
            Key             =   ""
         EndProperty
         BeginProperty ListImage11 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":2206
            Key             =   ""
         EndProperty
         BeginProperty ListImage12 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":2558
            Key             =   ""
         EndProperty
         BeginProperty ListImage13 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
            Picture         =   "frmMain.frx":28AA
            Key             =   ""
         EndProperty
      EndProperty
   End
   Begin VB.Menu mnuFile
      Caption         =   "&File"
      Begin VB.Menu mnuFileNew
         Caption         =   "&New"
         Shortcut        =   ^N
      End
      Begin VB.Menu mnuFileOpen
         Caption         =   "&Open"
         Shortcut        =   ^O
      End
      Begin VB.Menu mnuFileClose
         Caption         =   "&Close"
      End
      Begin VB.Menu GetFile
         Caption         =   "Get File"
      End
      Begin VB.Menu mnuFileBar1
         Caption         =   "-"
      End
      Begin VB.Menu mnuFileSave
         Caption         =   "&Save"

Shortcut = ^S

End
      Begin VB.Menu mnuFileSaveAs
         Caption         =   "Save &As..."
      End
      Begin VB.Menu mnuFileSaveAll
         Caption         =   "Save A&ll"
      End
      Begin VB.Menu mnuFileBar2
         Caption         =   "-"
      End
      Begin VB.Menu mnuFileProperties
         Caption         =   "Propert&ies"
      End
      Begin VB.Menu mnuFileBar3
         Caption         =   "-"
      End
      Begin VB.Menu mnuFilePageSetup
         Caption         =   "Page Set&up..."
      End
      Begin VB.Menu mnuFilePrintPreview
         Caption         =   "Print Pre&view"
      End
      Begin VB.Menu mnuFilePrint
         Caption         =   "&Print..."
         Shortcut        =   ^P
      End
      Begin VB.Menu mnuFileBar4
         Caption         =   "-"
      End
      Begin VB.Menu mnuFileSend
         Caption         =   "Sen&d..."
      End
      Begin VB.Menu mnuFileBar5
         Caption         =   "-"
      End
      Begin VB.Menu mnuFileMRU
         Caption         =   ""
         Index           =   0
         Visible         =   0   'False
      End
      Begin VB.Menu mnuFileMRU
         Caption         =   ""
         Index           =   1
         Visible         =   0   'False
      End
      Begin VB.Menu mnuFileMRU
         Caption         =   ""
         Index           =   2
         Visible         =   0   'False
      End
      Begin VB.Menu mnuFileMRU
         Caption         =   ""
         Index           =   3
         Visible         =   0   'False
      End
      Begin VB.Menu mnuFileBar6
         Caption         =   "-"
         Visible         =   0   'False
      End
      Begin VB.Menu mnuFileExit
         Caption         =   "E&xit"
      End
   End
   Begin VB.Menu mnuEdit
      Caption         =   "&Edit"
      Begin VB.Menu mnuEditUndo
         Caption         =   "&Undo"
         Shortcut        =   ^Z
      End
      Begin VB.Menu mnuEditBar1
         Caption         =   "-"
      End
      Begin VB.Menu mnuEditCut
         Caption         =   "Cu&t"
         Shortcut        =   ^X
      End
      Begin VB.Menu mnuEditCopy
         Caption         =   "&Copy"
         Shortcut        =   ^C
      End
      Begin VB.Menu mnuEditPaste
         Caption         =   "&Paste"
         Shortcut        =   ^V
      End
      Begin VB.Menu mnuEditPasteSpecial
         Caption         =   "Paste &Special..."
      End
   End
   Begin VB.Menu mnuHelp
      Caption         =   "&Help"
      Begin VB.Menu mnuHelpContents
         Caption         =   "&Contents"
      End
      Begin VB.Menu mnuHelpSearch
         Caption         =   "&Search For Help On..."
      End

Begin VB.Menu mnuHelpBar1

Caption         =   "-"
      End
      Begin VB.Menu mnuHelpAbout
         Caption         =   "&About HTTPer..."
      End
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function OSWinHelp% Lib "user32" Alias "WinHelpA" (ByVal hwnd&, ByVal HelpFile$, ByVal wCommand%, dwData As Any)
Private Sub Form_Load()
    Me.Left = GetSetting(App.Title, "Settings", "MainLeft", 1000)
    Me.Top = GetSetting(App.Title, "Settings", "MainTop", 1000)
    Me.Width = GetSetting(App.Title, "Settings", "MainWidth", 6500)
    Me.Height = GetSetting(App.Title, "Settings", "MainHeight", 6500)
End Sub
Private Sub Form_Unload(Cancel As Integer)
    If Me.WindowState <> vbMinimized Then
        SaveSetting App.Title, "Settings", "MainLeft", Me.Left
        SaveSetting App.Title, "Settings", "MainTop", Me.Top
        SaveSetting App.Title, "Settings", "MainWidth", Me.Width
        SaveSetting App.Title, "Settings", "MainHeight", Me.Height
    End If
End Sub
Private Sub GetFile_Click()
    Dim binarydata() As Byte
    RichTextBox1.Text =
        Inet1.OpenURL("http://www.microsoft.com/vbasic/default.htm")
    binarydata() =
        Inet1.OpenURL("http://www.server.com/museums/image.jpg", icByteArray)
    Open "E:\vb\image.jpg" For Binary Access Write As #1
    Put #1, , binarydata()
    Close #1
    MsgBox "Finished."
End Sub
Private Sub mnuHelpAbout_Click()
    'To Do
    MsgBox "About Box Code goes here!"
End Sub
Private Sub tbToolBar_ButtonClick(ByVal Button As ComctlLib.Button)
    Select Case Button.Key
        Case "New"
            mnuFileNew_Click
        Case "New"
            mnuFileNew_Click
        Case "Open"
            mnuFileOpen_Click
        Case "Save"
            mnuFileSave_Click
        Case "Print"
            mnuFilePrint_Click
        Case "Cut"
            mnuEditCut_Click
        Case "Copy"
            mnuEditCopy_Click
        Case "Paste"
            mnuEditPaste_Click
        Case "Bold"
            'To Do
            MsgBox "Bold Code goes here!"
        Case "Italic"
            'To Do
            MsgBox "Italic Code goes here!"
        Case "Underline"
            'To Do
            MsgBox "Underline Code goes here!"
        Case "Left"
            'To Do
            MsgBox "Left Code goes here!"
        Case "Center"
            'To Do
            MsgBox "Center Code goes here!"
        Case "Right"

'To Do

MsgBox "Right Code goes here!"
    End Select
End Sub
Private Sub mnuHelpContents_Click()
    Dim nRet As Integer
    'if there is no helpfile for this project display a message to the user
    'you can set the HelpFile for your application in the
    'Project Properties dialog
    If Len(App.HelpFile) = 0 Then
        MsgBox "Unable to display Help Contents. There is no Help associated with this project.", vbInformation, Me.Caption
    Else
        On Error Resume Next
        nRet = OSWinHelp(Me.hwnd, App.HelpFile, 3, 0)
        If Err Then
            MsgBox Err.Description
        End If
    End If
End Sub
Private Sub mnuHelpSearch_Click()
    Dim nRet As Integer
    'if there is no helpfile for this project display a message to the user
    'you can set the HelpFile for your application in the
    'Project Properties dialog
    If Len(App.HelpFile) = 0 Then
        MsgBox "Unable to display Help Contents. There is no Help associated with this project.", vbInformation, Me.Caption
    Else
        On Error Resume Next
        nRet = OSWinHelp(Me.hwnd, App.HelpFile, 261, 0)
        If Err Then
            MsgBox Err.Description
        End If
    End If
End Sub
Private Sub mnuEditCopy_Click()
    'To Do
    MsgBox "Copy Code goes here!"
End Sub
Private Sub mnuEditCut_Click()
    'To Do
    MsgBox "Cut Code goes here!"
End Sub
Private Sub mnuEditPaste_Click()
    'To Do
    MsgBox "Paste Code goes here!"
End Sub
Private Sub mnuEditPasteSpecial_Click()
    'To Do
    MsgBox "Paste Special Code goes here!"
End Sub
Private Sub mnuEditUndo_Click()
    'To Do
    MsgBox "Undo Code goes here!"
End Sub
Private Sub mnuFileOpen_Click()
    Dim sFile As String
    With dlgCommonDialog
        'To Do
        'set the flags and attributes of the
        'common dialog control
        .Filter = "All Files (*.*)|*.*"

.ShowOpen

If Len(.filename) = 0 Then
            Exit Sub
        End If
        sFile = .filename
    End With
    'To Do
    'process the opened file
End Sub
Private Sub mnuFileClose_Click()
    'To Do
    MsgBox "Close Code goes here!"
End Sub
Private Sub mnuFileSave_Click()
    'To Do
    MsgBox "Save Code goes here!"
End Sub
Private Sub mnuFileSaveAs_Click()
    'To Do
    'Set up the common dialog control
    'prior to calling ShowSave
    dlgCommonDialog.ShowSave
End Sub
Private Sub mnuFileSaveAll_Click()
    'To Do
    MsgBox "Save All Code goes here!"
End Sub
Private Sub mnuFileProperties_Click()
    'To Do
    MsgBox "Properties Code goes here!"
End Sub
Private Sub mnuFilePageSetup_Click()
    dlgCommonDialog.ShowPrinter
End Sub
Private Sub mnuFilePrintPreview_Click()
    'To Do
    MsgBox "Print Preview Code goes here!"
End Sub
Private Sub mnuFilePrint_Click()
    'To Do
    MsgBox "Print Code goes here!"
End Sub
Private Sub mnuFileSend_Click()
    'To Do
    MsgBox "Send Code goes here!"
End Sub
Private Sub mnuFileMRU_Click(Index As Integer)
    'To Do
    MsgBox "MRU Code goes here!"
End Sub
Private Sub mnuFileExit_Click()
    'unload the form
    Unload Me

End Sub

Private Sub mnuFileNew_Click()
    'To Do
    MsgBox "New File Code goes here!"
End Sub

That's it for our coverage of the FTP and HTTP protocols for the moment. In this chapter we've worked with powerful methods of interacting with the Internet using these protocols. We've discussed the Execute() method, FTP commands such as CD, MKDIR, GET, PUT, and DIR, and the HTTP commands GET, HEAD, POST, and PUT, as well as the OpenURL() method, which handles a great many of the details. In the next chapter, we'll continue our exploration of Visual Basic on the Internet when we turn to email.

© 1997 by Steven Holzner. All rights reserved.