
Get HTML pages with mswinsock.winsock
Dim winsock, InData, TotalBytesIn, DocLength
TotalBytesIn = 0
Main
Sub Main()
set winsock = Wscript.createobject("MSWINSOCK.Winsock", "winsock_")
winsock.Remotehost = "www.ahwayside.com"
winsock.RemotePort = 80 'HTTP PORT FOR REQUESTING HTML DOC
winsock.connect
Dim secs 'seconds
While winsock.state <> 7 And winsock.state <> 8 And winsock.state <> 9 And
secs <> 25
Wscript.Sleep 1000 'Sleep for a sec then check again
secs = secs + 1
Wend
If secs > 24 Then
MsgBox "Timed Out"
Wscript.Quit
End If
RequestedDoc = "GET / HTTP/1.0" & vbcrlf _
& "Accept: application/vnd.ms-powerpoint, application/vnd.ms-excel,
application/msword, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
*/*" & vbcrlf _
& "Accept-Language: en-us" & vbcrlf _
& "Accept-Encoding: gzip, deflate" & vbcrlf _
& "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)" & vbcrlf _
& "Host: 148.75.157.206" & vbcrlf _
& "Cache-Control: max-stale=0" & vbcrlf _
& "Connection: close" & vbcrlf & vbcrlf
winsock.SendData RequestedDoc
WScript.Sleep 25000
MsgBox "Done requesting but I didnt receive a response. Winsock State: " &
" " & winsock.state
If winsock.state <> 0 Then winsock.close
Set winsock = nothing
End Sub
Function WriteData(Data) 'Just used mainly for logging info
'note: its set up for win98 not nt
Dim fso, file
Set fso = createobject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\windows\desktop\winsock.txt", 8, True)
file.write Data & vbcrlf
file.close
Set file = nothing
Set fso = nothing
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' WINSOCK EVENTS '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub winsock_DataArrival(bytesTotal)
winsock.GetData InData, VBString
WriteData InData
TotalBytesIn = TotalBytesIn + Cint(bytesTotal)
'Check if the string contains info about the data sent
If Instr(InData, "Content-Length: ") Then
DocLength = PullBetween(InData, "Content-Length: ", vbcrlf)
DocLength = Right(DocLength, Len(DocLength) - InStr(DocLength, " "))
End If
If CInt(TotalBytesIn) > CInt(DocLength) Or CInt(TotalBytesIn) =
CInt(DocLength) Then
MsgBox "File done downloading!" & vbcrlf & "Total bytes in:" &
TotalBytesIn & vbcrlf & "HTML Bytes in: " & DocLength
If winsock.state <> 0 Then winsock.close
Set winsock = nothing
Wscript.Quit
End If
End Sub
Sub winsock_Error(Number, Description, SCode, Source, HelpFile, HelpContext,
CancelDisplay)
MsgBox "Error " & Number & vbcrlf & Description
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' PROCESSING FUNCTIONS '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function PullBetween(InData, BSearchStr, ESearchStr)
Dim MyStr
Dim FoundStr
MyStr = InData
FoundStr = Mid(MyStr, InStr(MyStr, BSearchStr) + 1, InStr(InStr(MyStr,
BSearchStr) + 1, MyStr, ESearchStr) - InStr(MyStr, BSearchStr) - 1)
PullBetween = FoundStr
End Function
Bryan Martin
Quote:
> Can anyone give me a short example about how to get an HTML page
> with "mswinsock.winsock" object ?
> Please mail me too.