Microsoft & .NET Visual Basic Using the Internet Transfer Control: Part 1

Using the Internet Transfer Control: Part 1

There are many reasons why you might want
to include the web in your application, and most of them will include
retrieving data – this means that you will need to good control to
perform these operations. This is where the Inet control steps in. It
comes with Visual Basic Pro and Enterprise editions, and can be very
useful.

To load the control, open VB, click
Project, Components. Scroll down and select Microsoft Internet
Transfer Control.

The Internet Transfer Control exposes some
useful properties. First of all, the AccessType property. This can
have three different values. There are as follows:

  1. icUseDefault
  2. icDirect
  3. icNamedProxy

icUseDefault is the one you will use most
of the time, but icDirect and icNamedProxy allow you to really make
your application very flexible. The Inet control exposes a Proxy
property which allows you to specify the name/IP address of a computer
to go through to get data.

When it comes to protocols, the Inet
control has them all! Just take a look:

  1. icUnknown
  2. icDefault
  3. icFTP
  4. icGopher
  5. icHTTP
  6. icHTTPS

Most of the time you will be using icHTTP
and icFTP, although you may stray across the others at some time. Most
of the names are self explanatory, so I won’t bother saying what each
one does. The control also gives the following properties:

  • URL – The complete URL of what you want to
    communicate with. e.g. http://www.vbsquare.com/
  • RemoteHost – The name/IP address of the
    remote host you are connecting with.
  • RemotePort – The port to connect on. For
    HTTP connections, you want port 80. For FTP, port 21.
  • Document – The name of an individual
    document
  • UserName – Username to login with
  • Password – Password that goes with the
    username

Pretty cool huh? Take a look at the
following list of ports to get you on your way:

  • HTTP: – 80
  • FTP: – 21
  • NNTP: 119
  • Telnet: – 23
  • SMTP: – 25
  • Gopher: – 70
  • POP 3: – 110
  • IRC: – 6667

Obviously these are only default ports,
and you should check with the host computer when attempting to
connect.

So, now that you have all this wonderful
information, how do you use the control? Well, for demonstration
purposes I will show you how to download a web page.

When downloading data, you will use the
Inet controls OpenURL method. It takes two parameters, URL and
DataType. Both are optional, so you can set them using the properties
and omit them when you make the call. Unfortunately the Inet control
does not provide a string data type, so we use the one closest to it,
named icByteArray. But this means we have to work a bit with the
returned data. We can use the UBound function to work with the
bytearray data, and write it to a string value. Take a look:

Dim b() As Byte
Dim intCount As Integer
Dim strData As String

Inet1.Cancel ' Stops any current operations

b() = Inet1.OpenURL("http://www.vbsquare.com/ _
      index.html", icByteArray)

For intCount = 0 To UBound(b) - 1

  strData = strData & Chr(b(intCount))

Next intCount

And if you wanted to save the data to a
file instead of a string variable, just use:

Open "myfile.txt" For Binary Access Write As #1

Put #1, , b()

Close #1

There we go. A nice simple web page
downloader.

If you think you can do any better (which
I am sure you can) then email me the files and I will post them.
Please, give me some feedback on this HowTo. I am trying to get the
balance right, am I doing it? Are you interested in more internet
comms? Or do you want more database / client/server information? Did
you think this HowTo was too basic or too advanced? Too long or too
short? Tell me what you thought at [email protected]
Tell me what you are working on at the moment, and what you want to
see in this newsletter: [email protected]
or use the online feedback form at http://www.vbsquare.com/feedback.htm

Latest Posts

Related Stories