Retrieving web page source, using OstroSoft Winsock Component (oswinsck.dll)
Download project source code
Minimum requirements: Visual Basic 5, oswinsck.dll*
* If you don't have OstroSoft Winsock Component, see installation instructions
1. In Visual Basic create a new Standard EXE project
2. Add a Reference to oswinsck
3. Add controls to the form: txtSource (TextBox), cmdView (CommandButton), txtURL (TextBox), txtProxy (TextBox), chkProxy (Checkbox), Label1 (Label)
4. Enter the following code:
Option Explicit
Dim sServer As String
Dim sPage As String
Dim WithEvents oWinsock As OSWINSCK.Winsock
Private Sub cmdView_Click()
  Dim nPort As Long
  
  txtSource.Text = ""
  nPort = 80
  sServer = Trim(txtURL.Text)
  If InStr(sServer, "://") > 0 Then_
    sServer = Mid(sServer, InStr(sServer, "://") + 3)
  If InStr(sServer, "/") > 1 Then
    sPage = Mid(sServer, InStr(sServer, "/"))
    sServer = Left(sServer, InStr(sServer, "/") - 1)
    If InStr(sPage, "#") > 1 Then _
      sPage = Left(sPage, InStr(sPage, "#") - 1)
  Else
    sPage = "/"
  End If
  If InStr(sServer, ":") > 1 Then
    nPort = Mid(sServer, InStr(sServer, ":") + 1)
    sServer = Left(sServer, InStr(sServer, ":") - 1)
  End If
  
  Set oWinsock = Nothing
  Set oWinsock = CreateObject("OSWINSCK.Winsock")
  If chkProxy.Value = 0 Then
    oWinsock.Connect sServer, nPort
  Else
    sPage = "http://" & sServer & sPage
    oWinsock.Connect txtProxy.Text, nPort
  End If
End Sub
Private Sub oWinsock_OnClose()
  oWinsock.CloseWinsock
  Set oWinsock = Nothing
End Sub
Private Sub oWinsock_OnConnect()
  oWinsock.SendData "GET " & sPage & " HTTP/1.0" & vbCrLf & vbCrLf
End Sub
Private Sub oWinsock_OnDataArrival(ByVal bytesTotal As Long)
  Dim sBuffer As String
  oWinsock.GetData sBuffer
  txtSource.Text = txtSource.Text & sBuffer
End Sub
Private Sub oWinsock_OnError(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)
  Debug.Print "Error " & Number & ": " & Description
End Sub
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment