home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / YouTube_Do2112345102008.psc / DGSwsHTTP.cls < prev    next >
Text File  |  2008-05-05  |  5KB  |  158 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "DGSwsHTTP"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  15. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  16. ' DGSwsHTTP Class Module by RegX
  17. ' Copyright 2002 DGS
  18. ' You may freely use this code as long as
  19. ' All Copyright information remains intact
  20. '=============================================
  21. 'Requires:
  22. 'cAppendString.cls
  23. 'Microsoft Scripting Runtime
  24. 'MSWinsockLib.Winsock (Mswinsck.ocx)
  25. '=============================================
  26. 'Description
  27. 'Makes downloading HTML files with progress a snap
  28. 'Exposes Response Header and FileData seperately
  29.  
  30. 'Usage ---------------------------------------------------------------
  31. 'Dim WithEvents wsc As DGSwsHTTP
  32. 'Set wsc = New DGSwsHTTP
  33. 'wsc.geturl "http://www.microsoft.com/"
  34. '---------------------------------------------------------------------
  35. Option Explicit
  36. Dim cstring As cAppendString
  37. Dim WithEvents ws As MSWinsockLib.Winsock
  38. Attribute ws.VB_VarHelpID = -1
  39. Public Event ProgressChanged(ByVal bytesreceived As Long)
  40. Public URL As String
  41. Private mvarRemotePort As String
  42. Private mvarRemoteHost As String
  43. Public totalbytesreceived As Long
  44. Public Event DownloadComplete()
  45. Public FileSize As Variant
  46. Public ResponseHeaderString As String
  47. Private dicResponseHeader As Dictionary
  48. Public filedata As Variant
  49. Public ResponseCode As String
  50. Public Event httpError(errmsg As String, Scode As String)
  51.  
  52. Public Property Let RemoteHost(ByVal vData As String)
  53.     mvarRemoteHost = vData
  54. End Property
  55.  
  56. Public Property Get RemoteHost() As String
  57.     RemoteHost = mvarRemoteHost
  58. End Property
  59.  
  60. Public Property Let RemotePort(ByVal vData As String)
  61.     mvarRemotePort = vData
  62. End Property
  63.  
  64. Public Property Get RemotePort() As String
  65.     RemotePort = mvarRemotePort
  66. End Property
  67.  
  68.  
  69. Public Sub geturl(URL As String)
  70.             If URL & "" = "" Then RaiseEvent httpError("No URL specified", 0)
  71.             cstring.Clear
  72.             Dim RHstart As Long
  73.             Dim RHend As Long
  74.             Dim RH As String
  75.             RHstart = InStr(1, URL, "://", vbTextCompare)
  76.             If RHstart = 0 Then RHstart = 1 Else RHstart = RHstart + 3
  77.             If Len(URL) <= RHstart + 3 Then
  78.                 RaiseEvent httpError("Invalid URL", 0)
  79.                 Exit Sub
  80.             End If
  81.             RHend = InStr(RHstart + 3, URL, "/", vbTextCompare)
  82.             If RHend = 0 Then RHend = Len(URL) + 1
  83.             RH = Mid(URL, RHstart, RHend - (RHstart))
  84.             
  85.             Me.URL = URL
  86.             ws.Close
  87.             ws.RemotePort = Me.RemotePort
  88.             ws.RemoteHost = RH
  89.             ws.Connect
  90. End Sub
  91.  
  92. Private Sub Class_Initialize()
  93. Set ws = New MSWinsockLib.Winsock
  94. Set cstring = New cAppendString
  95. Set dicResponseHeader = New Dictionary
  96. Me.RemotePort = 80
  97. End Sub
  98.  
  99. Private Sub Class_Terminate()
  100. Set dicResponseHeader = Nothing
  101. Set cstring = Nothing
  102. Set ws = Nothing
  103. End Sub
  104.  
  105. Private Sub ws_Close()
  106. filedata = cstring.Value
  107. cstring.Clear
  108. RaiseEvent DownloadComplete
  109. End Sub
  110.  
  111. Private Sub ws_Connect()
  112.     Me.totalbytesreceived = 0
  113.     Me.FileSize = 0
  114.     Me.ResponseHeaderString = ""
  115.     Me.filedata = ""
  116.         Dim Request As String
  117.         Request = "GET " & Me.URL & " HTTP/1.0" & vbCrLf & vbCrLf
  118.         ws.SendData Request
  119. End Sub
  120.  
  121. Private Sub ws_DataArrival(ByVal bytesTotal As Long)
  122. dicResponseHeader.RemoveAll
  123. On Error Resume Next
  124. Dim data As String
  125. Dim arrheader() As String
  126. Dim headkeys() As String
  127. Dim headvals() As String
  128. Dim Item As Variant
  129. Dim X As Long
  130. ws.GetData data
  131.         If totalbytesreceived = 0 Then
  132.             ResponseHeaderString = Left(data, InStr(1, data, vbCrLf & vbCrLf, vbTextCompare) + 2)
  133.             totalbytesreceived = bytesTotal - (Len(ResponseHeaderString) + 2)
  134.             cstring.Append Mid(data, Len(ResponseHeaderString) + 2)
  135.             arrheader = Split(ResponseHeaderString, vbCrLf, -1, vbTextCompare)
  136.             For Each Item In arrheader
  137.                 X = InStr(1, Item, " ", vbTextCompare)
  138.                 If X > 1 Then
  139.                     dicResponseHeader.Add Left(Item, X - 1), Mid(Item, X + 1)
  140.                     Debug.Print Left(Item, X - 1) & Mid(Item, X + 1)
  141.                 End If
  142.             Next
  143.                     ResponseCode = Left(dicResponseHeader.Items(0), 3)
  144.                     Me.FileSize = dicResponseHeader.Item("Content-Length:")
  145.                     If ResponseCode <> "200" Then
  146.                         RaiseEvent httpError(CStr(dicResponseHeader.Items(0)), ResponseCode)
  147.                     End If
  148.         Else
  149.         cstring.Append data
  150.             totalbytesreceived = totalbytesreceived + bytesTotal
  151.             RaiseEvent ProgressChanged(totalbytesreceived)
  152.         End If
  153. End Sub
  154.  
  155. Private Sub ws_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)
  156. RaiseEvent httpError(Description, CStr(Scode))
  157. End Sub
  158.