Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Handling HTTP Operations In Visual Basic

There are two ways of handling HTTP operations with the Microsoft Internet transfer control: using the OpenUrl method and using the Execute method. The OpenUrl method lets you download files and uses the HTTP protocol if the URL you specify begins with http:// (for example, “http://www.microsoft.com”). Here’s how you use OpenUrl:

InetControl.OpenUrl  url [, datatype]

The datatype argument can either be icString (the default) for text data or icByteArray for binary data. If you use icString, OpenUrl returns a string; if you use icByteArray, OpenUrl returns a byte array.

The Execute method can execute HTTP commands. Here’s how you use Execute:

InetControl.Execute  url, operation, data, requestHeaders

The arguments for the Execute method are as follows:

  url—String that specifies the URL to which the control should connect. If no URL is specified here, the URL specified in the URL property will be used.
  operation—String that specifies the type of operation to be executed.
  data—String that specifies the data for operations.
  requestHeaders—String that specifies additional headers to be sent from the remote server. The format for these is header name: header value vbCrLf.

The HTTP commands that you can use with the Internet transfer control and what they do appear in Table 21.4.

Table 21.4 HTTP commands of the Internet transfer control’s Execute method.
Command Description
GET Gets the file named in URL (for example, Execute “http://www.server.com/index.htm”, “GET”)
HEAD Gets headers of file given in URL property (for example, Execute , “HEAD”)
POST Provides additional data to support request to host (for example, Execute , “POST”, strFormData)
PUT Replaces data at URL (for example, Execute , “PUT”, “new.htm”)

Let’s see an example. Add an Internet transfer control, Inet1, to a program, as well as a text box, Text1. We’ll download the HTML of the Microsoft Visual Basic Web page using the HTTP protocol and display that page in the text box, so set Text1’s MultiLine property to True and its Scrollbars property to Both (3). In addition, we can download a binary file—an image file, for example—using the HTTP protocol and store that image file on disk. To let the user perform these actions, add two buttons to the program: Command1, with the caption “Read HTML”, and Command2, with the caption “Read binary”.

When the user clicks Command1, the Read HTML button, we just download the raw HTML of the Microsoft Visual Basic Web page and display it in the text box Text1 using the OpenURL method (note that by the time you read this, Microsoft may possibly have renamed this file):

Private Sub Command1_Click()
    Text1.Text = _
        Inet1.OpenURL("http://www.microsoft.com/vbasic/default.htm")
End Sub

When the user clicks Command2, the Read Binary button, we can read a binary file using the HTTP protocol. In this case, we’ll read a GIF file from the Microsoft Web site, home.gif, which just displays the word “Microsoft”. We load that image file into a byte array, bytData:

Private Sub Command2_Click()
    Dim bytData() As Byte

    bytData() = Inet1.OpenURL(_
    "http://www.microsoft.com/library/images/gifs/toolbar/home.gif", _
         icByteArray)
    ...
End Sub

All that’s left is to write the file out to disk and to inform the user that the operation is complete, which we do with a message box:

Private Sub Command2_Click()
    Dim bytData() As Byte

    bytData() = Inet1.OpenURL(_
    "http://www.microsoft.com/library/images/gifs/toolbar/home.gif", _
         icByteArray)

    Open "c:\vbbb\http\home.gif" For Binary Access Write As #1
    Put #1, , bytData()
    Close #1

    MsgBox "home.gif downloaded"
End Sub

That’s it. Run the program now, as shown in Figure 21.16. When you click the Read HTML button, the program downloads the Microsoft Visual Basic Web page and displays it in the text box, as shown in Figure 21.16. When you click the Read Binary button, the program downloads the home.gif file onto disk. Our http program is a success.


Figure 21.16  Downloading a Web page’s HTML using the HTTP protocol.

The code for this example is located in the http folder on this book’s accompanying CD-ROM.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.