Get HTML pages with mswinsock.winsock 
Author Message
 Get HTML pages with mswinsock.winsock

Can anyone give me a short example about how to  get  an HTML page
with "mswinsock.winsock" object ?
Please mail me too.


Tue, 18 Jan 2005 18:14:16 GMT  
 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.



Tue, 18 Jan 2005 20:52:08 GMT  
 Get HTML pages with mswinsock.winsock
thanks, it works fine.
Do you know if winsock object have a method o property tu use a proxy server?
In msdn site i don't found something about this.
Thanks again for the code.
Quote:

> 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



> > Can anyone give me a short example about how to  get  an HTML page
> > with "mswinsock.winsock" object ?
> > Please mail me too.



Thu, 20 Jan 2005 16:50:01 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. GetData using MSWinsock.Winsock

2. Creating an instance of MSWinsock.Winsock gives error 0x80040112

3. ActiveX Object - MSWinsock.Winsock

4. Authentification sur une page ASP et HTML par Winsock

5. doing winsock with the dll instead of the mswinsock control

6. Getting Source HTML from a Web Page

7. winsock and getting my script to wait for it

8. How Do I Script Tables and make page breaks form a html Page

9. using VBA to get HTML pages and pull info from those pages

10. How to PRINT All pages of HTML Report from ans ASP Page

 

 
Powered by phpBB® Forum Software