|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Handling HTTP Operations In Visual BasicThere 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). Heres 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. Heres how you use Execute: InetControl.Execute url, operation, data, requestHeaders The arguments for the Execute method are as follows:
The HTTP commands that you can use with the Internet transfer control and what they do appear in Table 21.4.
Lets see an example. Add an Internet transfer control, Inet1, to a program, as well as a text box, Text1. Well download the HTML of the Microsoft Visual Basic Web page using the HTTP protocol and display that page in the text box, so set Text1s MultiLine property to True and its Scrollbars property to Both (3). In addition, we can download a binary filean image file, for exampleusing 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, well 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 thats 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
Thats 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.
The code for this example is located in the http folder on this books accompanying CD-ROM.
|
|
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. |