Option Explicit
Dim mAgent
As New cMAgent
Private Sub Form_Load()
'Óñòàíàâëèâåì ñâÿçè ñ ñîêåòÊîíòðîëîì
Set VBSocketContainer =
Me 'mTransferByVBSocket
mAgent.LinkToSocketControl =
Me.T1
'cMAgent
mAgent.AgentServer = DownloadFileToVar("mrim.mail.ru", 443)
'Îïðåäåëÿåì ñåðâåð ìÀãåíòà
Debug.
Print mAgent.AgentServer
End Sub
Private Sub T1_DataArrival(Data
As String)
Select Case T1.Tag
Case "
ownloadFileToVar"
'mTransferByVBSocket
If ResivingLen = 0
Or Len(Trim$(Data)) = 0
Then
'Ïîëó÷àåì ðàçìåð çàêà÷èâàåìûõ äàííûõ
Dim S
As Long, e
As Long
S = InStr(1, Data, "Content-Length:", 1)
S = S +
Len("Content-Length:"
e = InStr(S, Data, vbCrLf, 1)
ResivingLen = VBA.Val(VBA.Trim$(VBA.
Mid$(Data, S, e)))
'Åñëè íå ïîëó÷èëè, ñ÷èòàåì ÷òî ýòî âñå ÷òî åñòü...
If ResivingLen = 0
Then ResivingLen = Len(Data)
End If
'Ñèãíàëèçàòîð ïðèõîäà äàííûõ
Shape1.Visible =
True 'Ìàãàþùàÿ ëàìïî÷êà ( òàéìåðå äåëàåòñÿ False)
AllDownloadedData = AllDownloadedData & Data
End Select
End Sub
Private Sub Timer1_Timer()
'Ìèãàíèå ëàìïî÷êè ïðè¸ìà äàííûõ
Shape1.Visible =
False
End Sub
Option Explicit
Private Declare Sub Sleep
Lib "kernel32.dll" (
ByVal dwMilliseconds
As Long)
Public ResivingLen
As Long 'Îáùèé ðàçìåð çàêà÷èâàåìûõ äàííûõ
Public AllDownloadedData
As String 'Çàêà÷àííûå äàííûå íà òåêóùèé ìîìåíò
Public VBSocketContainer
As Object
'Ô-öèÿ çàêà÷êè ôàéëà ÷åðåç VBSocket
Public Function DownloadFileToVar(strFile
As String,
Optional lPort
As Long = 80)
As String
Dim Reqest
As String, strHost
As String
Dim S
As Long
Dim timeout
As Integer
VBSocketContainer.Timer1.Enabled =
True
'Ïîëó÷àåì èìÿ õîñòà èç àäðåñà
S = VBA.InStr(1, strFile, "://", 1) + 3
timeout = VBA.InStrB(S, strFile, "/", 1) - 1
If S = 3
Then S = 1:
If timeout < 1
Then timeout =
Len(strFile)
strHost = VBA.
Mid$(strFile, S, timeout)
'Îáíóëÿåì ðàáî÷èå ïåðåìåííûå
timeout = 0: S = 0: VBSocketContainer.T1.Tag = "
ownloadFileToVar": ResivingLen = 0: AllDownloadedData = ""
VBSocketContainer.T1.AlertMode = Yes
'Ïîêàçûâàòü îøèáêè
VBSocketContainer.T1.Connect strHost, lPort
'Ïîäêëþ÷àåìñÿ
Do While VBSocketContainer.T1.State = Connecting
DoEvents
Call Sleep(100&
timeout = timeout + 1
If timeout > 200
Then '20 cåê
MsgBox "Íåò îòâåòà îò " & strHost, vbCritical, "Timeout"
 
ownloadFileToVar = ""
Exit Function
End If
Loop 'Æä¸ì ïîäêëþ÷åíèÿ
'Ôîðìèðóåì http çàïðîñ
Reqest$ = "GET " & strFile & " HTTP/1.1" & vbCrLf & _
"Host: " & strHost & vbCrLf & _
"Referer: " & strHost & vbCrLf & _
"Proxy-Connection: Keep-Alive" & vbCrLf & _
"Content-
Type: application/x-www-form-urlencoded" & vbCrLf & _
"Pragma: no-cache" & vbCrLf & vbCrLf
VBSocketContainer.T1.SendData Reqest$
'Îòïðàâëÿåì åãî
timeout = 0
'Ñ÷¸ò÷èê òàéìàóòà ñáðàñóåì
'Æä¸ì ïîêà ïîëó÷åííûå äàííûå áóäóò áîëüøå ÷åì ðàçìåð êîòîðûé âåðíóë íàì ñåðâåð ResivingLen
'ýòî áóäåò îçíà÷àòü ÷òî âñå äàííûå ñêà÷àíû, íó èëè ïî÷òè âñå ò.ê. òàì åù¸ + çàãîëîâîê ïàêåòà
Do Until Len(AllDownloadedData) >= ResivingLen
And ResivingLen > 0
DoEvents
Call Sleep(100&
timeout = timeout + 1
If timeout > 200
Then '20 cåê
MsgBox "Íåò îòâåòà îò " & strHost, vbCritical, "Timeout"
 
ownloadFileToVar = ""
Exit Function
End If
Loop
Call Sleep(1000&
'Öóòü-öóòü æä¸ì, ÷òîáû òî÷íî âñå çàãðóçèëèñü
DoEvents
VBSocketContainer.T1.CloseConnection
S = InStr(1, AllDownloadedData, vbCrLf & vbCrLf, 1)
'Îïðåäåëÿåì êîíåö çàãîëîâêà
If S <> 0
Then S = S + 4
Else S = 1
 
ownloadFileToVar = VBA.
Mid$(AllDownloadedData, S, ResivingLen)
'È îòðåçàåì
AllDownloadedData = ""
ResivingLen = 0
VBSocketContainer.Timer1.Enabled =
False
End Function