Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:Using The Internet Controls
function GetCookie (name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
var end = document.cookie.indexOf (";", j);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(j, end));
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
var m1='';
var gifstr=GetCookie("UsrType");
if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; }
document.write(m1+m2+m3);
Keyword
Title
Author
ISBN
Publisher
Imprint
Brief
Full
Advanced Search
Search Tips
Please Select
-----------
Components
Content Mgt
Certification
Databases
Enterprise Mgt
Fun/Games
Groupware
Hardware
IBM Redbooks
Intranet Dev
Middleware
Multimedia
Networks
OS
Prod Apps
Programming
Security
UI
Web Services
Webmaster
Y2K
-----------
New Titles
-----------
Free Archive
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
function isIE4()
{
return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') );
}
function bookMarkit()
{
var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0";
parent.location.href=url;
//var win = window.open(url,"myitk");
//if(!isIE4())
// win.focus();
}
Search this book:
Previous
Table of Contents
Next
Getting Chunks
Now lets get back to the problem of retrieving data from the controls buffer. This is done with the GetChunk method. The syntax is as follows:
Inet1.GetChunk( size [,DataType] )
The size argument specifies how many bytes of data to retrieve; it is a type Long. The optional DataType argument specifies whether the data is text or binary. You use the same values for this argument as with the OpenURL method: icString (the default, value = 0) and icByteArray (value = 1). If you try to retrieve more bytes of data than are present in the buffer, youll simply get whatever is there. If you retrieve only part of the data in the buffer, the remainder of the data stays in the buffer ready to be retrieved with the next call to GetChunk.
Remember, however, that we are dealing here with asynchronous data retrieval. This means that when you execute one of the data retrieval methods that places data in the Internet Transfer controls buffer, program execution continues while the control waits for the data. How, then, do you know when the data has been received and is waiting in the buffer to be retrieved with GetChunk?
The answer lies in the StateChanged event. Two of the possible events, as described earlier, can signal the successful receipt of data: icResponseReceived (value = 8) and icResponseCompleted (value = 12). The technique is to place the calls to GetChunk in the StateChanged event procedure, executing them if, and only if, one of these states has occurred. To allow for the possibility that more than one call to GetChunk will be necessary to retrieve all the data in the buffer, we can use a loop. Listing 18.2 shows the code, which displays the full buffer contents in a Rich Text Box control named RTB1.
Listing 18.2 Using GetChunk in the StateChanged event procedure to retrieve data from the Internet Transfer Control buffer.
Private Sub Inet1_StateChanged(ByVal State As Integer)
Dim s1, s2
Select Case State
Other cases not shown.
Case icResponseCompleted
s1 =
s2 =
Do
s1 = Inet1.GetChunk(512, icString)
s2 = s2 & s1
Loop Until s1 =
RTB1.Text = s2
End Select
End Sub
The icResponseReceived state does not always mean that there is data in the buffer. Some operations, such as handshaking with an FTP site, result in this state without any data being placed in the buffer. If you wait for icResponseCompleted, you can be sure that any and all data has been received.
The Execute Method
Perhaps the most important method youll use with the Internet Transfer control is Execute. With this method, you can send a variety of commands to the remote computer. The syntax is:
Inet1.Execute (url, operation, data, requestHeaders)
The url argument specifies the remote computer that the command will be sent to. This argument is optional; if it is omitted, the URL specified in the controls URL property is used. The operation argument specifies the command to be sent, as explained later. The remaining two arguments are used only with certain commands.
The commands that you can send with the Execute method depend on the connection protocolFTP or HTTP. For an HTTP connection, four commands are available:
GETRetrieves data from the server
HEADRetrieves the header only from the server
POSTSends additional data required to complete a request
PUTUploads a file to the server
Use of the POST and PUT commands is beyond the scope of this book. If you want to use these commands, you need to be fairly knowledgeable about the details of HTTP.
When using the HEAD command, you will receive the HTTP headers, which are intercepted by the control and do not appear in the buffer. The most common use for the HEAD command is to verify that a URL is functioning without the overhead of retrieving the entire content with GET. Simply execute HEAD, and if the operation succeeds (as indicated by the Response Completed value being detected in the StateChanged event procedure), you know the URL is functional.
The Internet Transfer control is most commonly used with GET. When using Execute with GET, the data returned by the server is placed in the Internet Transfer controls internal buffer. You retrieve the data using the GetChunk method, as explained earlier. Here are some examples of using Execute with GET.
To retrieve the entire default Microsoft home page:
Inet.Execute <http://www.microsoft.com> , GET
To retrieve the books page from my Web site:
Inet1.Execute <http://www.pgacon.com/books.htm>, GET
You can use the GET command to submit data to Common Gateway Interface (CGI) scriptsfor example, submitting queries to Web search engines. You need to know the proper syntax, of course. For example, the following command searches the Yahoo site for references to bananas.
Inet1.Execute <http://search.yahoo.com/bin/search?p=banana> , GET
Types Of Errors
The many things that can go wrong when using the Internet Transfer control can be divided into two categories:
Certain situations generate an error in the control, resulting in the StateChanged event procedure being passed the icError value. Examples include trying to connect to a nonexistent Web site, or if your connection to the Internet is not functioning properly.
Other situations may not generate an error in the control, even though something is not working properly. For instance, if you request a nonexistent file from a functioning Web site, no error is generated; instead, the HTTP host returns an Object Not Found message. Even when a control error does not occur, your program needs to be on the alert for messages returned from the server indicating that something has gone wrong.
An HTTP Demonstration
The program presented in this section demonstrates using the Internet Transfer control for HTTP transfers. It is not intended to perform any task beyond helping you gain some familiarity with how the control works. The program is shown in Figure 18.1. The form contains a Rich Text Box control to display the information returned from the server, and a Text Box to display status messages as reported by the StatusChanged event procedure. Other elements include Text Boxes and Option Buttons to permit you to specify the URL and the command to be sent. Of course, an Internet Transfer control is on the form as well. I have enabled this program to use all four of the HTTP-related commandsGET, HEAD, PUT, and POSTso you can experiment with them. More details on the programs objects and properties are presented in Listing 18.3.
Figure 18.1 The Internet Transfer control demonstration program.
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.
Wyszukiwarka
Podobne podstrony:
Ustawa z dnia 18 03 2008r o zmianie ustawy o rachunkowości18 03 Szkolenia przeciwpozarowe v1 118 03 200418 03 M Śmiechowski Wentylacja nieinwazyjna i oscylacyjna18 03 08 sem Vwyklad organizacja 18 03 201318 03 14 PietrzykWyklad Inf 18 03 2009KNP 18 0318 03 08 sem Vetykieta 16 03 18 15 29Michnik nie chce zeznawać Nasz Dziennik, 2011 03 18TI 99 03 18 T B pl(1)Więcej miliarderów, więcej żebraków Nasz Dziennik, 2011 03 182005 03 18więcej podobnych podstron