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 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 18
Using The Internet Controls

Visual Basic supplies you with controls to perform the most commonly needed Internet tasks.

Once you become familiar with the Visual Basic way of programming, your first thought when faced with a particular programming task is to look for an existing component to do the job. What about writing Internet-capable programs? The same approach works here, too. Microsoft provides two components, the Internet Transfer control and the WebBrowser control, that encapsulate all of the Internet functionality most programmers will ever need. In this chapter, we will take a detailed look at the Internet Transfer control and a quick look at the WebBrowser control.

Unfortunately, these two controls are available only with the Professional and Enterprise editions of Visual Basic. As of this writing, Microsoft did not plan to include them with the Learning Edition. The WebBrowser control is also part of the installation of Microsoft Internet Explorer 4.x.

Internet programming with Visual Basic (or any other development tool, for that matter) can be divided into two areas:

  Writing applications that use the Internet, but do not run on the Internet—An example would be a word processing program that permits users to open and save documents on a remote Internet server.
  Writing applications that actually run on the Internet—An example would be a program that lets users access a catalog over the Internet and place orders.

The controls covered in this chapter are used to write the first type of application. Writing applications that run on the Internet is possible with the Professional and Enterprise editions of Visual Basic, but that topic is beyond the scope of this book.

The Internet Transfer Control

Microsoft has encapsulated a lot of commonly needed Internet functionality in the Internet Transfer control (ITC). Specifically, this control provides capabilities for Hypertext Transfer Protocol (HTTP) and File Transfer Protocol (FTP) transfers. You don’t have the flexibility you would if you were programming directly, making the required Winsock and other calls, but for most programming needs, the Internet Transfer control provides all you need—and, it’s a lot easier.

The Internet Transfer control is not a replacement for a Web browser. This control can be used to transfer information between client and server using HTTP or FTP protocols, but it does not display Web pages in their proper format (or in any format at all, for that matter), does not permit navigation of links, and so on. If you want to retrieve the text of a Web page for processing, this is the control to use. For example, you could use the Internet Transfer control as the basis for an automated Web search engine, downloading the contents of pages and looking for specified keywords. If you want to incorporate browsing capabilities into your Visual Basic application, use the WebBrowser control (covered later in this chapter).


TIP:  New And Improved

If you have tried to use earlier versions of the Internet Transfer control, you probably came away very frustrated. The first couple of releases of this control were buggy, and programmers found them difficult or impossible to use. I am happy to report that the current ITC is much improved and seems finally to have all of its bugs worked out.


The Internet Transfer control is an “invisible” control, much like Visual Basic’s Timer control. It is not seen while the program is executing, but rather, exists behind the scenes doing its job.

Using The Internet Transfer Control For HTTP

HTTP is the foundation for the World Wide Web. Web pages are written in Hyper-text Markup Language (HTML), and HTTP is the protocol by which Web pages are transferred from place to place. Every time someone logs onto a Web site, HTTP is at work. HTML files are standard text files that can be viewed and edited with any text editor. What makes them special is that they contain HTML codes that control the formatting and display of the contents in a Web browser.

Don’t let the term protocol intimidate you. A protocol is nothing more than a set of rules about how some task is to be accomplished. HTTP is the universally accepted specification for transferring HTML documents over a network. The inner workings of HTTP are somewhat complex, but the ITC hides all the details from you.

HTTP is a relatively simple protocol, which is one factor that probably has contributed to its popularity and widespread adoption. It is a stateless protocol, meaning that no persistent connection is established between the client and the server. The client sends a request that is addressed to the server; when the server receives the request, it sends a response addressed to the client. A good analogy is the postal system, where you can send a letter and receive a reply without ever establishing a discrete connection between you and your correspondent. A telephone conversation is a good example of a protocol that is not stateless, because a discrete connection is established and maintained throughout the session.

The simplest way to use the Internet Transfer control is with the OpenURL method. Here’s how it works (in this and other examples, we will assume the form includes an Internet Transfer control named Inet1):

Inet1.OpenURL(URLName, DataType)

URLName is the full URL that you want to open; it can be either an FTP or HTTP server (FTP is covered in more detail later in the chapter). The DataType argument specifies whether you are retrieving text or binary data. Possible values are icString (the default, value = 0) and icByteArray (value = 1). The method’s return value is the complete data returned by the site. The data is returned synchronously, meaning that program execution pauses until the data has been retrieved. This is an important point, as we will see later.

The data returned by the OpenURL method should be put in a string variable or a byte array (or the equivalents), depending on the setting of the DataType argument. The following code retrieves the text contents of the default file at the indicated site (which happens to be my Web page) and displays it in a Text Box:

Text1.Text = Inet1.OpenURL(“<http://www.pgacon.com>”, icString)

If you want to retrieve a specific file, specify it in the URL:

Text1.Text = Inet1.OpenURL(“<http://www.pgacon.com/books.htm>”, icString)

While the OpenURL method is convenient and easy to use, I suggest you stay away from it. Why? There are two reasons:

  OpenURL operates synchronously, which means that code following the method call does not execute until the request is complete. Using the Internet Transfer control’s other methods (which I will explain soon) provides asynchronous operation, in which your program can be doing other tasks while the Internet Transfer control executes the command. The control then notifies the program, by means of an event, when it is finished.
  Using methods other than OpenURL provides you with greater flexibility. You can, for example, retrieve just the header of a Web page instead of the entire page.


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. Read EarthWeb's privacy statement.