VB projects - Get HTML Source
Description: Get HTML source of specified URL, using Winsock control
Minimum requirements: VB5 Pro
Download: source code
Screenshot:

Project: Standard EXE
ActiveX Controls/Objects: MSWINSCK.OCX
Controls: cmdGet (CommandButton), txtURL (TextBox), Timer1 (Timer), txtSource (TextBox), Winsock1 (Winsock)
Code:
Dim sURL As String
Dim sHost As String
Dim sPage As String
Dim lPort As Long
Private Sub GetHTMLSource(ByVal sURL As String)
txtSource = ""
sHost = Mid(sURL, InStr(sURL, "://") + 3)
If InStr(sHost, "/") > 0 Then
sPage = Mid(sHost, InStr(sHost, "/"))
sHost = Left(sHost, InStr(sHost, "/") - 1)
Else
sPage = "/"
End If
If InStr(sHost, ":") > 0 Then
lPort = Mid(sHost, InStr(sHost, ":") + 1)
sHost = Left(sHost, InStr(sHost, ":") - 1)
Else
lPort = 80
End If
With Winsock1
If .State <> sckClosed Then .Close
.RemoteHost = sHost
.RemotePort = lPort
.Connect
End With
End Sub
Private Sub cmdGet_Click()
GetHTMLSource txtURL
End Sub
Private Sub Timer1_Timer()
Winsock1.Close
txtSource = "Connection timeout"
End Sub
Private Sub Winsock1_Connect()
Timer1.Enabled = True
Winsock1.SendData "GET " & sPage & " HTTP/1.0" & Chr(10) & Chr(10)
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Timer1.Enabled = False
Dim sBuffer As String
Winsock1.GetData sBuffer
txtSource = txtSource & sBuffer
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, _
ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, _
ByVal HelpContext As Long, CancelDisplay As Boolean)
Winsock1.Close
txtSource = "Error " & Number & ": " & Description
End Sub
|