Link to home
Start Free TrialLog in
Avatar of huang4044
huang4044

asked on

How to automate accessing web using VB?

Hi!
I am a beginner for VB, and I am writing a program which
can log into the web site using my ID and password and
download some information or interact with the web.

Can anyone tell me is VB the right tool or what method
can I use to achieve this purpose?

Thanks,
Dav.
Avatar of JonFish85
JonFish85

Use the Inet Control. Project -> Components -> Microsoft Inet Control. Then something like this:

Inet1.Execute "ftp://username:password@ftp.host.com", "CMD"

once it is connected, then you can go to something like:

Inet1.Execute , "PWD" etc.... Look in MSDN for more info as Im on my way out the door
Avatar of huang4044

ASKER

Hi!Jon,
can you explain more detail about how to
use Inet Control.Project?
thanks,
Dav.
start your VB standard exe with a set of component tools side bar on your left. Right click on the side bar & select components. U'll see a list of built-in VB components. Look for Microsoft Internet Transfer Control 6.0 & tick on it. U'll see a new component appear on the side bar.

simply select that component & place it on your form. Double click on that component & u can start coding.

good luck.

see what you can get from this code I used:
'%%%%%%% This is in a module %%%%%%%%%%%%%%%%

Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Public Const MAX_PATH = 260

Public Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

Public Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * MAX_PATH
        cAlternate As String * 14
End Type

Public IsConnected As Boolean

Public Sub ShowFiles(data As String, lst As ListBox)
Dim cnt As Long
Dim temp As String
Dim StartAt As Long
Dim OldAt As Long

  lst.Clear
  lst.AddItem "Parent Directory"
  StartAt = InStr(data, vbCrLf)
  OldAt = 1
  Do While StartAt <> 0
    temp = Mid$(data, OldAt, StartAt - OldAt)
    temp = Replace(temp, vbCrLf, "")
    If Right$(temp, 1) = "/" Then
      temp = Left$(temp, Len(temp) - 1)
      temp = temp & " (Folder)"
      lst.AddItem temp
    ElseIf UCase(Right(temp, 4)) = ".URT" Then
      lst.AddItem temp
    End If
    DoEvents
    OldAt = StartAt
    StartAt = InStr(OldAt + 1, data, vbCrLf)
  Loop
End Sub

Public Function SearchForFiles(strFilename As String, Ext As String, lst As ListBox) As Boolean
Dim hSearch As Long
Dim success As Long
Dim buffer As String
Dim FindInfo As WIN32_FIND_DATA

lst.Clear
If Ext = "" Then Ext = "*"
If Left(Ext, 1) <> "." Then Ext = "." & Ext
hSearch = FindFirstFile(strFilename & "*" & Ext, FindInfo)
If hSearch = -1 Then Exit Function

Do
  buffer = Left(FindInfo.cFileName, InStr(FindInfo.cFileName, vbNullChar) - 1)
  lst.AddItem buffer
  success = FindNextFile(hSearch, FindInfo)
Loop Until success = 0

FindClose hSearch
If lst.ListCount > 0 Then SearchForFiles = True Else SearchForFiles = False
End Function

'%%%%%%%%%%%%%%%% End Module %%%%%%%%%%%%%%%%%%%%

'%%%%%%%%%%%%%%%% This is in the form's code %%%%

Dim CDir As String
Dim lngInterval As Long
Dim strUsername As String
Dim strPassword As String
Dim strFilename As String
Dim strExtension As String
Dim strHost As String


Private Sub cmdLogon_Click()
  If cmdLogon.Caption = "&Log In" Then
    If (txtHost.Text = "") Or (txtUsername.Text = "") Or (txtPassword.Text = "") Or (txtTimer.Text = "") Then
      MsgBox "Please fill on all the information required!", , "Error"
      Exit Sub
    End If
   
    If Dir(txtFileDir.Text) = "" Then
      MsgBox "You specified an invalid directory!", , "Error"
      Exit Sub
    End If
 
    lngInterval = Val(txtTimer.Text) * 1000
    strUsername = txtUsername.Text
    strPassword = txtPassword.Text
    strFilename = txtFileDir.Text
    strHost = txtHost.Text
    strExtension = cboExt.Text
    cmdLogon.Caption = "&Exit"
    tmrCheck.Interval = lngInterval
    tmrCheck_Timer
  Else
    Unload Me
  End If
End Sub

Private Sub Command1_Click()
  Inet.UserName = "Username"
  Inet.Password = "Password"
  Inet.Execute LOGON, "PWD"
  Exit Sub
End Sub

Private Sub Inet_StateChanged(ByVal State As Integer)
Dim data As String
  Select Case State
    Case icConnected
      'Connected to FTP
      IsConnected = True
    Case icResponseReceived
      'Got a response
    Case icDisconnected
      'Disconnected
      IsConnected = False
    Case icError
      MsgBox Inet.ResponseCode & " : " & Inet.ResponseInfo
    Case icResponseCompleted
      data = Inet.GetChunk(2048, icString)
      If data = "/" Then
        lblIndex.Caption = "Index Of '" & data & "'"
        CDir = CDir & data
        Inet.Execute , "DIR"
      ElseIf data = "" Then
        Inet.Execute , "DIR"
      Else
        ShowFiles data, lstFiles
      End If
  End Select
End Sub


Private Sub lstFiles_DblClick()
  If Inet.StillExecuting Then Exit Sub
  If Not IsConnected Then Exit Sub
  If InStr(lstFiles.List(lstFiles.ListIndex), "(Folder)") Then
    CDir = CDir & Left(lstFiles.List(lstFiles.ListIndex), Len(lstFiles.List(lstFiles.ListIndex)) - Len(" (Folder)")) & "/"
    CDir = Replace(CDir, "//", "/")
    CDir = Replace(CDir, "///", "/")
    lblIndex.Caption = "Index Of '" & CDir & "'"
    Inet.Execute , "CD " & Left$(lstFiles.List(lstFiles.ListIndex), Len(lstFiles.List(lstFiles.ListIndex)) - Len(" (Folder)")) & "/"
  ElseIf UCase(lstFiles.List(lstFiles.ListIndex)) = "PARENT DIRECTORY" Then
    Dim temp() As String
    Dim cnt As Long
    temp = Split(CDir, "/")
    CDir = ""
    For cnt = LBound(temp) To (UBound(temp) - 2)
      CDir = CDir & temp(cnt) & "/"
    Next cnt
    CDir = Replace(CDir, "//", "/")
    CDir = Replace(CDir, "///", "/")
    'MsgBox CDir
    lblIndex.Caption = "Index Of '" & CDir & "'"
    Inet.Execute , "CDUP"
  End If
End Sub

Private Sub sckNoop_Timer()
  If IsConnected And Not Inet.StillExecuting Then
    Inet.Execute , "NOOP"
  End If
End Sub

Private Sub tmrCheck_Timer()
Dim ConnStr As String

  If Inet.StillExecuting Then Exit Sub
  If IsConnected Then Exit Sub
  If SearchForFiles(strFilename, strExtension, lstDir) = True Then
    'Found at least one file
    ConnStr = "ftp://" & strUsername & ":" & strPassword & "@" & strHost
    Inet.Execute ConnStr, "PWD"
  Else
    'Nothing found
  End If
End Sub

'%%%%%%%%%%%%%%%%%% End Form Code %%%%%%%%%%%%%%%%%%%%


Hopefully that will help
ASKER CERTIFIED SOLUTION
Avatar of JonFish85
JonFish85

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!I think the code will helps!By the way,hard to
believe you are just 15!
Yeah, people seem to have a hard time believing it. Im also going to try to get my MCSD this year. Im taking the first test tomorrow, Desktop Apps with VB6 (70-176), and I had better pass, cause its $100 a test :-)