Internet Programming (HTTP and FTTP)

Tips >> Visual Basic

MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE

 

The WebBrowser and Internet Transfer Controls facilitate the development of Internet programs.

The WebBrowser Control

The WebBrowser control provides the functionality of Internet Explorer through point and click hyperlinks. To use the WebBrowser Control, add it to the ToolBox by selecting the Components menu item and select Microsoft Internet Controls from the list. The use of the control is self-explanitory as can be seen in the following example.

Option Explicit
Private Sub cmdBack_Click()
     On Error Resume Next
     webBrowser.GoBack
End Sub
Private Sub cmdForward_Click()
     On Error Resume Next
webBrowser.GoForward
End Sub
Private Sub cmdHome_Click()
     On Error Resume Next
     webBrowser.GoHome
End Sub
Private Sub cmdRefresh_Click()
     On Error Resume Next
     webBrowser.Refresh
End Sub
Private Sub cmdStop_Click()
     lblStatus.Caption = "Interrupted"
     webBrowser.Stop
End Sub
Private Sub Form_Activate()
     webBrowser.Width = Me.ScaleWidth
     webBrowser.Height = Me.ScaleHeight - webBrowser.Top
     fraNavigation.Width = Me.ScaleWidth
     txtURL.Width = Me.ScaleWidth - txtURL.Left - 500
End Sub
Private Sub Form_Load()
     webBrowser.Navigate "https://www.juicystudio.com"
End Sub
Private Sub txtURL_GotFocus()
     txtURL.SelStart = 0
     txtURL.SelLength = Len(txtURL.Text)
End Sub
Private Sub txtURL_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
          KeyAscii = 0
          webBrowser.Navigate txtURL.Text
     End If
End Sub
Private Sub webBrowser_DocumentComplete(ByVal pDisp As Object, URL As Variant)
     txtURL.Text = URL
End Sub
Private Sub webBrowser_DownloadBegin()
     lblStatus.Caption = "Loading ..."
End Sub
Private Sub webBrowser_DownloadComplete()
     lblStatus.Caption = "Loaded"
End Sub


THE INTERNET CONTROL

The Internet Transfer Control supports the Hypertext Transfer Protocol (HTTP) and File Transfer Protocol (FTP) networking protocols. To use the Internet Transfer Control, add it to the ToolBox by selecting the Components menu item and select Microsoft Internet Transfer Control 6.0 from the list.

Hypertext Transfer Protocol (HTTP)

To retrieve a file using HTTP is simply a case of using the Internet Transfer Control's OpenURL method. The actual file is retrieved, rather than the formatted HTML.
The following example has an Internet Transfer control named itcHTTP and two TextBoxes named txtURL and txtFile. The txtFile's MultiLine Property is set to True.

Option Explicit
Private Sub txtFile_GotFocus()
     txtURL.SetFocus
End Sub
Private Sub txtURL_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
          KeyAscii = 0
          txtFile.Text = "Downloading file " & txtURL.Text & "..."
          On Error GoTo URLError
          txtFile.Text = itcHTTP.OpenURL(Trim(txtURL.Text))
     End If
     Exit Sub
URLError:
     MsgBox Err.Description, vbExclamation, "OpenURL"
     txtFile.Text = "Unable to open " & txtURL.Text
     txtFile.Text = txtFile.Text & " - " & Err.Description
End Sub

 

 

FILE TRANSFER PROTOCOL (FTP)

FTP Commands can be sent through the Internet Transfer Control's Execute method. The Execute method executes a request on the remote server. The first parameter specifies the URL, and the second parameter specifies the operation to be performed. The URL parameter is optional and if not provided the Internet Transfer Control's URL property will be used. The GetChunk method reads the data returned.
The following example requests the current directory and writes it to a Label called lblRemoteDirectory.

itcFTP.Execute "ftp.juicystudio.com", "pwd"
lblRemoteDirectory.Caption = itcFTP.GetChunk(1024, icString)

 

DETERMINING THE FTP STATE:

The StateChanged Event fires when there is a change in the connection. The Select Case construct can be used to determine the state and what action to take.
The following example writes the state to a Label called lblStatus when the StateChanged Event fires.

Private Sub itcFTP_StateChanged(ByVal State As Integer)
Select Case State
     Case icResolvingHost
          lblStatus.Caption = "Resolving Host"
     Case icHostResolved
          lblStatus.Caption = "Host Resolved"
     Case icConnecting
          lblStatus.Caption = "Connecting ..."
     Case icConnected
          lblStatus.Caption = "Connected"
     Case icRequesting
          lblStatus.Caption = "Requesting ..."
     Case icRequestSent
          lblStatus.Caption = "Request Sent"
     Case icReceivingResponse
          lblStatus.Caption = "Receiving ..."
     Case icResponseReceived
          lblStatus.Caption = "Response Received"
     Case icDisconnecting
          lblStatus.Caption = "Disconnecting ..."
     Case icDisconnected
          lblStatus.Caption = "Disconnected"
     Case icError
          lblStatus.Caption = itcFTP.ResponseInfo
     Case icResponseCompleted
          lblStatus.Caption = "Operation Complete"
     End Select
End Sub

 

FTP PROPERTIES:

The Properties of the Internet Transfer Control may be used to set up the FTP Remote Address Settings. The URL, UserName, and Password properties may be used to set up the remote address.

itcFTP.URL = txtAddress.Text
itcFTP.UserName = txtUsername.Text
itcFTP.Password = txtPassword.Text

 

The execute command will use the URL property if the parameter for the URL is missing.

itcFTP.Execute , "dir"

 

 

FTP EXAMPLE PROGRAM

The following is a fully working FTP program that uses the Internet Transfer Control.

Option Explicit
Private Sub cmdConnect_Click()
     ' Use the caption property to determine whether to connect or disconnect
     If Left(cmdConnect.Caption, 4) = "&Con" Then
          connectHost
     Else
          disconnectHost
     End If
End Sub
Private Sub cmdDelete_Click()
     Dim strOperation As String
     Dim strFilename As String
     Dim response As Integer, counter As Integer
     response = MsgBox("Are you sure", vbQuestion + vbYesNo, "Delete")
     If response = vbYes Then
          For counter = 0 To lstRemote.ListCount - 1
          If lstRemote.Selected(counter) = True Then
               strFilename = lstRemote.List(counter)
               ' Determine whether directory or file
               If Right(strFilename, 1) = "/" Then
                    strOperation = "rmdir " & Left(strFilename, Len(strFilename) - 1)
               Else
                    strOperation = "delete " & strFilename
               End If
               executeCommand strOperation, False
          End If
          Next counter
          listDir
     End If
End Sub
Private Sub cmdMkDir_Click()
     Dim dir As String, strOperation As String
     dir = InputBox("Enter directory name", "Make Directory")
     If dir <> "" Then
          strOperation = "mkdir " & dir
          executeCommand strOperation, True
     End If
End Sub
Private Sub cmdReceive_Click()
     Dim counter As Integer
     Dim strOperation As String
     Dim strFilename As String, outFile As String
     For counter = 0 To lstRemote.ListCount - 1
          If lstRemote.Selected(counter) = True Then
               strFilename = lstRemote.List(counter)
               If Len(dirList.Path) > 3 Then
                    outFile = dirList.Path & "\" & strFilename
               Else
                    outFile = dirList.Path & strFilename
               End If
               strOperation = "recv " & strFilename & " " & outFile
               executeCommand strOperation, False
               lstRemote.Selected(counter) = False
          End If
     Next counter
     filList.Refresh
End Sub
Private Sub cmdSend_Click()
     Dim counter As Integer
     Dim strOperation As String
     Dim strFilename As String, outFile As String
     For counter = 0 To filList.ListCount - 1
          If filList.Selected(counter) = True Then
               strFilename = filList.List(counter)
               outFile = lblRemoteDirectory.Caption & "/" & strFilename
               strOperation = "send " & strFilename & " " & outFile
               executeCommand strOperation, False
          End If
     Next counter
     listDir
     filList.Refresh
End Sub
Private Sub dirList_Change()
     filList.Path = dirList.Path
End Sub
Private Sub drvList_Change()
     On Error GoTo driveError
          dirList.Path = drvList.Drive
          Exit Sub
     driveError:
     MsgBox Err.Description, vbExclamation, "Drive Error"
End Sub
Private Sub Form_Unload(Cancel As Integer)
     If cmdSend.Enabled = True Then
          itcFTP.Execute , "quit"
     End If
End Sub
Private Sub itcFTP_StateChanged(ByVal State As Integer)
     Select Case State
          Case icResolvingHost
               lblStatus.Caption = "Resolving Host"
          Case icHostResolved
               lblStatus.Caption = "Host Resolved"
          Case icConnecting
               lblStatus.Caption = "Connecting ..."
          Case icConnected
               lblStatus.Caption = "Connected"
          Case icRequesting
               lblStatus.Caption = "Requesting ..."
          Case icRequestSent
               lblStatus.Caption = "Request Sent"
          Case icReceivingResponse
               lblStatus.Caption = "Receiving ..."
          Case icResponseReceived
               lblStatus.Caption = "Response Received"
          Case icDisconnecting
               lblStatus.Caption = "Disconnecting ..."
          Case icDisconnected
               lblStatus.Caption = "Disconnected"
          Case icError
               lblStatus.Caption = itcFTP.ResponseInfo
               txtLog.Text = txtLog.Text & itcFTP.ResponseInfo & vbCrLf
          Case icResponseCompleted
               lblStatus.Caption = "strOperation Complete"
          txtLog.Text = txtLog.Text & "Operation Complete" & vbCrLf
     End Select
     txtLog.SelStart = Len(txtLog.Text)
End Sub
Private Sub lstRemote_DblClick()
     Dim strOperation As String, dir As String
     ' If item is a directory, change to that directory
     If Right(lstRemote.List(lstRemote.ListIndex), 1) = "/" Then
          dir = lstRemote.List(lstRemote.ListIndex)
          strOperation = "cd " & Left(dir, Len(dir) - 1)
          executeCommand strOperation, True
     End If
End Sub
Private Sub txtLog_GotFocus()
     txtAddress.SetFocus
End Sub
Private Sub connectHost()
     itcFTP.URL = txtAddress.Text
     itcFTP.UserName = txtUsername.Text
     itcFTP.Password = txtPassword.Text
     listDir
     cmdSend.Enabled = True
     cmdReceive.Enabled = True
     cmdMkDir.Enabled = True
     cmdDelete.Enabled = True
     lstRemote.Enabled = True
     cmdConnect.Caption = "D&isconnect from Remote Host"
     Exit Sub
     connectError:
     MsgBox Err.Description, vbExclamation, "FTP"
End Sub
Private Sub disconnectHost()
     Dim strOperation As String
     itcFTP.Execute , "quit"
     strOperation = "quit"
     executeCommand strOperation, False
     cmdSend.Enabled = False
     cmdReceive.Enabled = False
     cmdMkDir.Enabled = False
     cmdDelete.Enabled = False
     lstRemote.Enabled = False
     cmdConnect.Caption = "&Connect to Remote Host"
End Sub
Private Sub executeCommand(ByVal op As String, ByVal ld As Boolean)
     If itcFTP.StillExecuting Then
          itcFTP.Cancel
     End If
     txtLog.Text = txtLog.Text & "Command: " & op & vbCrLf
     itcFTP.Execute , op
     finishCommand
     If ld = True Then
          listDir
          finishCommand
     End If
End Sub
Private Sub finishCommand()
     Do While itcFTP.StillExecuting
          DoEvents
     Loop
End Sub
Private Sub listDir()
     Dim strOperation As String
     Dim data As Variant, counter As Integer
     Dim start As Integer, length As Integer
     start = 1
     lstRemote.Clear
     strOperation = "dir"
     executeCommand strOperation, False
     Do
          data = itcFTP.GetChunk(1024, icString)
          DoEvents
          For counter = 1 To Len(data)
               If Mid(data, counter, 1) = Chr(13) Then
                    If length > 0 And Mid(data, start, length) <> "./" Then
                         lstRemote.AddItem Mid(data, start, length)
                    End If
                    start = counter + 2
                    length = -1
               Else
                    length = length + 1
               End If
          Next counter
     Loop While LenB(data) > 0
     strOperation = "pwd"
     executeCommand strOperation, False
     lblRemoteDirectory.Caption = itcFTP.GetChunk(1024, icString)
End Sub

 

If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

 

E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641