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



TIP:  Get Connected

For this or any other Internet program to work, you must be connected to the Internet. If you connect via a local area network, then your connection should be ready to use at all times (assuming you have configured it properly). If you connect via a modem, you must use the Windows Dial Up Networking feature to connect to your Internet Service Provider.


Listing 18.3 Objects and properties in ITC_DEMO.FRM.

Begin VB.Form Form1
  Begin VB.TextBox txtHeaders
    Height=285
    Left=2280
    TabIndex=14
    Top=5280
    Width=3495
End
Begin VB.Frame Frame1
    Caption=“Command”
    Begin VB.OptionButton optPUT
      Caption=“Put”
End
Begin VB.OptionButton optHead
      Caption=“HEAD”
End
Begin VB.OptionButton optPost
      Caption=“POST”
End
Begin VB.OptionButton optGet
      Caption=“GET”
      Value=-1  ‘True
End
End
Begin VB.TextBox txtInput
End
Begin VB.TextBox txtURL
End
Begin VB.CommandButton Command1
      Caption=“Quit”
      Index=1
End
Begin VB.CommandButton Command1
      Caption=“Execute”
      Index=0
End
Begin VB.TextBox txtStatus
End
Begin RichTextLib.RichTextBox RT1
End
Begin InetCtlsObjects.Inet Inet1
End
Begin VB.Label Label4
      Alignment=1  ‘Right Justify
      Caption=“Request headers”
Begin VB.Label Label3
      Alignment=1  ‘Right Justify
      Caption=“Input data”
End
Begin VB.Label Label2
      Alignment=1  ‘Right Justify
      Caption=“URL”
End
Begin VB.Label Label1
      Alignment=1  ‘Right Justify
      Caption=“Status”
End
End

The program’s code is shown in Listing 18.4. When the user clicks on the Execute button, the program gathers the URL and command information from the various controls and uses the Execute method to send the resulting command to the specified URL. The response, if any, is displayed in the box. The current state of the Internet Transfer control is displayed in the Status box. If an error occurs, a dialog box pops up with information about the error.

Listing 18.4 Code in ITC_DEMO.FRM.

Option Explicit

Private Sub Command1_Click(Index As Integer)

Select Case Index
    Case 0  ‘ Execute
        Command1(0).Enabled = False
        Call Execute
    Case 1  ‘ Quit
        End
End Select

End Sub

Private Sub Form_Load()

Inet1.Protocol = icHTTP

End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)

Dim S1 As String, s2 As String

Select Case State
    Case icResolvingHost ‘ 1
        txtStatus.Text = “Looking up IP address of host computer”
    Case icHostResolved  ‘ 2
        txtStatus.Text = “IP address found”
    Case icConnecting ‘ 3
        txtStatus.Text = “Connecting to host computer”
    Case icConnected ‘ 4
        txtStatus.Text = “Connected”
    Case icRequesting ‘ 5
        txtStatus.Text = “Sending a request to host computer”
    Case icRequestSent ‘ 6
        txtStatus.Text = “Request sent”
    Case icReceivingResponse ‘ 7
        txtStatus.Text = “Receiving a response from host computer”
    Case icResponseReceived ‘ 8
        txtStatus.Text = “Response received”
    Case icDisconnecting ‘ 9
        txtStatus.Text = “Disconnecting from host computer”
    Case icDisconnected ‘ 10
        txtStatus.Text = “Disconnected”
    Case icError ‘ 11
        txtStatus.Text = “Error ” & Inet1.ResponseCode & _
          “ ” & Inet1.ResponseInfo
        MsgBox (txtStatus.Text)
        Command1(0).Enabled = True
    Case icResponseCompleted ‘ 12
        txtStatus.Text = “Request completed - all data received”
        S1 = “”
        s2 = “”
        Do
            S1 = Inet1.GetChunk(512, icString)
            s2 = s2 & S1
      Loop Until S1 = “”
      RT1.Text = s2
      Command1(0).Enabled = True
End Select

End Sub

Public Sub Execute()

Dim Operation As String

RT1.Text = “”
If optGet.Value Then Operation = “GET”
If optHead.Value Then Operation = “HEAD”
If optPost.Value Then Operation = “POST”
If optPut.Value Then Operation = “PUT”

If txtInput.Text <> “” And txtHeaders.Text <> “” Then
    Inet1.Execute txtURL.Text, Operation, _
        txtInput.Text, txtHeaders.Text
ElseIf txtInput.Text <> “” Then
    Inet1.Execute txtURL.Text, Operation, txtInput.Text
Else
    Inet1.Execute txtURL.Text, Operation
End If

End Sub

Using The Internet Transfer Control For FTP

FTP is similar to HTTP in that it is a protocol for transferring files between computers. It differs in not being a stateless protocol, because a control connection is maintained between client and server for the duration of an FTP session. It is also a lot more powerful than HTTP, with many more commands and security features.

FTP Logons

While HTTP servers require no logon, FTP servers do. Even if the server permits unrestricted access (called anonymous logon), your program must send a username and password. The username and password sent by the Internet Transfer control are determined by its Username and Password properties. The value of the Username property is sent to the remote server as the username for logon. If this property is blank (Null or “”), then “anonymous” is sent. The process is a bit more complicated with the Password property, as explained in Table 18.2.

There is one illegal combination: You cannot have a non-Null Password property combined with a Null Username property. Note that leaving both of these properties blank results in the standard anonymous FTP logon: a username of “anonymous” and a password consisting of your email address (as obtained from the system settings).

I have experienced a bug in the Internet Transfer control where it sometimes does not send “anonymous” as the username when the Username property is blank. Perhaps this problem will be fixed, but you can easily work around it by explicitly assigning the string “anonymous” to the Username property.

Using The OpenURL Method

The OpenURL method is the easiest way to perform some FTP tasks. To obtain the directory of an FTP site (the directory is an FTP site’s “default” file):

Text1.Text = Inet1.OpenURL(“<ftp://ftp.microsoft.com>”, icString)

To download a file from an FTP site, you need to use binary mode, unless you are sure it is a text file (although binary mode works fine for text files, too):

Dim buf() as Byte
buf() = Inet1.OpenURL(“<ftp://ftp.microsoft.com/anyfile.exe>” , _
    icByteData)

Then you can save the downloaded file to disk as follows:

Open “c:\programs\anyfile.exe"” For Binary as #1
Put #1,, buf()
Close #1


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.