Using the Internet Transfer Control
The Internet Transfer control implements two widely-used Internet
protocols: the HyperText Transfer Protocol (HTTP) and the File Transfer
Protocol (FTP). Using the Internet Transfer control, you can connect to
any site that uses one of these protocols, and retrieve files using either
the OpenURL or Execute method. www.tartoos.com
Possible Uses
-
To add an FTP browser to any application.
-
To create an application that automatically downloads files from a
public FTP site.
-
To parse a World Wide Web site for graphics references and download the
graphics only.
-
To present a custom display of dynamic data retrieved from a Web page.
Basic Operationwww.tartoos.com
The functionality of the Internet Transfer control depends on the protocol
you wish to use. Because the two supported protocols work differently, the
operations you can perform depend on which protocol you are using. For
example, the GetHeader method only works with HTTP (HTML documents).
However, there are a few procedures that are common to both protocols.
Basically, in order to use either protocol, you must:
-
Set the AccessType property to a valid proxy server.
-
Invoke the OpenURL method with a valid URL.
-
Invoke the Execute method with a valid URL and command appropriate to
the protocol.
-
Use the GetChunk method to retrieve data from the buffer.
www.tartoos.com
Setting the AccessType
Property: Using a Proxy Server
www.tartoos.com
In order to make any kind of connection to the Internet, you must
determine how your computer is connected to the Internet. If you are on an
intranet, you will probably be connected to the Internet via a proxy
server.
In short, a proxy server is an
intermediary between your computer and the Internet. All computers on an
intranet that need to connect to the Internet must do so through a proxy
server. Thus the proxy functions as a firewall between the intranet and
the Internet, discarding invalid end-user and external requests, thereby
protecting the intranet from hostile actions. www.tartoos.com
To find the proxy settings on your computer
Note The following steps apply only to Windows 95, Windows NT®
4.0, or later systems.
-
On the Taskbar of your computer, click Start.
-
On the Settings item, click the Control Panel.
-
Double-click the Internet icon.
-
On the Internet Properties dialog box, click Connection.
-
Under Proxy Server, confirm that the Connect Through a Proxy
Server check box is selected. www.tartoos.com
-
If it is selected, click Settings. The name of proxy servers you
use for various protocols will be found in the dialog box. If no proxy
is defined, contact your system administrator for available proxy
servers.
If you intend to use a proxy other than that named in the dialog box, set
the AccessType property to icNamedProxy (2). Then set the Proxy property
to the name of the proxy, as shown in the code below:
Inet1.Proxy = "myProxyName"
Inet1.AccessType = icNamedProxy www.tartoos.com
On the other hand, if you are content to use the default proxy (as
determined by your computer's registry), ignore the Proxy property, and
simply set the AccessType to icUseDefault (0).
The settings for AccessType are shown in the following table:www.tartoos.com
|
Constant
|
Value
|
Description
|
|
icUseDefault
|
0
|
(Default) Use Defaults. The control uses default settings found in
the registry to access the Internet.
|
|
icDirect
|
1
|
Direct to Internet. The control has a direct connection to the
Internet.
|
|
icNamedProxy
|
2
|
Named Proxy. Instructs the control to use the proxy server specified
in the Proxy property.
|
Invoke the OpenURL Method
After you set the AccessType property, the most basic operation is to use
the OpenURL method with a valid URL. When you use the OpenURL method, the
result will depend on the target URL. For example, the following URL will
return the HTML document found at www.microsoft.com:
' A
TextBox control named Text1 contains the
'
result of the method. The Internet Transfer
'
control is named Inet1.
Text1.Text = Inet1.OpenURL("http://www.microsoft.com")www.tartoos.com
As a result, the TextBox control is filled with the HTML source, which may
resemble the figure below:
In this case, the default action was to return the HTML document located
at the URL. However, if the URL is modified to target a specific text
file, the actual file would be retrieved. For example, the following code:
Text1.Text = Inet1. _
OpenURL("ftp://ftp.microsoft.com/disclaimer.txt")
would
result in the actual text of the file, as shown below: www.tartoos.com
Tip When you use either the OpenURL or Execute method, you need
not set the Protocol property. The Internet Transfer control will
automatically set itself to the correct protocol, as determined by the
protocol portion of the URL.
Finally, you can use the OpenURL method with a URL that includes appended
data. For example, many Web sites offer the ability to search a database.
To search, send a URL that includes the search criteria. For example, the
following code would use a search engine named "search.exe" with the
criteria "find=Maui."
Dim strURL As String
strURL = _
"http://www.megaphone43.com/cgi-bin/search.exe?find=maui
Text1.Text = Inet1.OpenURL(strURL)
If the search engine finds a match for the criteria, an HTML document
would be assembled and returned with the appropriate information.
Saving to a File Using the OpenURL Method
If you wish to save the data retrieved through the OpenURL method to a
file, use the Open, Put, and Close statements, as shown in the code below.
This example streams a binary file into a Byte array before saving the
data to disk:
Dim strURL As String
Dim bData() As Byte ' Data variable
Dim intFile As Integer ' FreeFile variable
strURL = _
"ftp://ftp.microsoft.com/Softlib/Softlib.exe"
intFile = FreeFile() ' Set intFile to an
unused
' file.
' The result of the OpenURL method goes into the Byte
' array, and the Byte array is then saved to disk.
bData() = Inet1.OpenURL(strURL, icByteArray)
Open "C:\Temp\Softlib.exe" For Binary Access Write _
As #intFile
Put #intFile, , bData()
Close #intFile
A similar procedure can be used to write a text file to disk, except no
Byte array is needed; the data is saved directly to the file:
Dim strURL As String ' URL string
Dim intFile As Integer ' FreeFile variable
IntFile = FreeFile()
strURL = "http://www.microsoft.com"
Open "c:\temp\MSsource.txt" For Output _
As #IntFile
Write #IntFile, Inet1.OpenURL(strURL)
Close #IntFile
|