Results 1 to 9 of 9

Thread: Winsock Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    4

    Winsock Question

    Hi,

    I'm trying to write my first application using Winsock. I'm trying to retrieve the HTML code from a website. Here is what I have:

    -------------------------------------------------
    Dim WithEvents Winsock As MSWinsockLib.Winsock

    Private Sub Form_Load()
    With Winsock
    .RemoteHost = "www.yahoo.com"
    .RemotePort = 80
    .Connect
    .SendData "GET /"
    End With
    End Sub

    Private Sub winsock_ondataarrival()
    Dim rBuffer

    rBuffer = Winsock.GetData
    MsgBox rBuffer
    End Sub
    -------------------------------------------------

    When I run it, I get:
    'Run-time error '91': Object variable of With block variable not set'

    When I click on 'Debug' it highlights the following line:
    '.RemoteHost = "www.yahoo.com"'

    Does anybody think they can point me in the right direction?
    Thanks!
    Tom

  2. #2
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Winsock Question

    you dont need winsock for that ..

    change strUrl to your own .. add a text box to display the contents .. with multiline lines and vertival scrollbars.

    add a command button ...

    Add Reference to MS XML version 2.0 (5.0 is the latest but certain hosts dont have it installed - I use 4.0 but same issue, so 2.0 is the safest that it is installed)

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim objHTTP As New MSXML.XMLHTTPRequest
    3.     objHTTP.Open "GET", strUrl, False
    4.     objHTTP.setRequestHeader "Content-Type", "text/html"
    5.     objHTTP.send
    6.     Text1.Text = objHTTP.responseText
    7. End Sub
    Last edited by rory; Jun 2nd, 2006 at 02:37 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    4

    Re: Winsock Question

    We have actually tried that and it works great. However, we figured we'd write it in Winsock to reduce some overhead. Our server will be performing this HTTP GET several times a second, so speed and resource usage are a priority.

    I just assumed using MSXML.XMLHTTPRequest could not possibly be as simple and pure as a plan HTML GET by itself. Knowing Microsoft, there are 50,000 lines of code inside MSXML.XMLHTTPRequest to perform the HTTP GET.

    Thoughts? Agree/disagree?

    Thanks!
    Tom

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    Re: Winsock Question

    I have an app that sends post messages to our IIS server to retrieve ASP pages and writes the html that is returned to html files back on the IIS server.

    I use the Winsock API rather than the Winsock control as the final version is a batch (non-console) app that runs in the scheduler on another server and does the conversions on the first of every month, converting pages that will display the previous month's data.

    Let me know if you think this will be of help and I'll email you the code. I have both the GUI version that I used to develop & test the code and the batch version.

  5. #5
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Winsock Question

    I dont know Tom, never thought about it as it works so fast :-)
    Though I'd like to know also ..

    I use the XML get also in ASP apps especially in an indexer for a search engine, basically loops through links from a database (few hundred) and grabs the HTML .. specifically keywords, title, etc .. to display for the search.

    As for VB im using the XMLHTTP to send data to an ASP page to do stuff then get the responses. Didnt think I could use winsock for that? CCoder id like to see the code if you dont mind .. i just did a patch program last night, it works fine with the way im doing it, but if I can use winsock for both the check on the AP page and also on the download of the files .. then would be better .. im using a Downloader Control built into the program.

  6. #6
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Winsock Question

    ok looked around, basically this is using the winsock control ..

    VB Code:
    1. Private Sub Form_Load()
    2.     With Winsock1
    3.         .Close
    4.         .LocalPort = 0
    5.         .Connect "www.myurl.com", 80
    6.     End With
    7. End Sub
    8.  
    9. Private Sub Winsock1_Connect()
    10.     Dim strData As String
    11.     strData = "GET / HTTP/1.1" & vbCrLf
    12.     strData = strData & "Host: www.myurl.com" & vbCrLf
    13.     strData = strData & "Connection: close" & vbCrLf
    14.     strData = strData & "Accept: */*" & vbCrLf
    15.     strData = strData & vbCrLf
    16.     Winsock1.SendData strData
    17.     Debug.Print strData
    18. End Sub
    19.  
    20. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    21.     Dim strData As String
    22.     Dim strHeader As String    
    23.     Winsock1.GetData strData
    24.     strHeader = Left$(strData, InStr(1, strData, vbCrLf & vbCrLf) - 1)
    25.     strData = Mid(strData, InStr(1, strData, vbCrLf & vbCrLf) + 4)
    26.     Debug.Print Trim$(strData)
    27. End Sub
    Last edited by rory; Jun 2nd, 2006 at 04:23 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    4

    Re: Winsock Question

    This code is exactly what I am 'trying' to do. My problem is, I don't know if there are any declarations (not even entirely sure what a declaration is) I need outside of this code. As explained in my first post, I get an error when trying to compile. That's actually what I can't figure out.

    When I run it, I get:
    'Run-time error '91': Object variable of With block variable not set'

    When I click on 'Debug' it highlights the following line:
    '.RemoteHost = "www.yahoo.com"'

    Help?!

    Tom

  8. #8
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Winsock Question

    Quote Originally Posted by tgill3764
    This code is exactly what I am 'trying' to do. My problem is, I don't know if there are any declarations (not even entirely sure what a declaration is) I need outside of this code. As explained in my first post, I get an error when trying to compile. That's actually what I can't figure out.

    When I run it, I get:
    'Run-time error '91': Object variable of With block variable not set'

    When I click on 'Debug' it highlights the following line:
    '.RemoteHost = "www.yahoo.com"'

    Help?!

    Tom
    RemoteHost doesnt understand Names

    So using your code it would connect like this . You dont send the data until it connects ...

    VB Code:
    1. Private Sub Form_Load()
    2.     With Winsock
    3.          .Connect "www.myurl.com", 80
    4.     End With
    5. End Sub

    also, why are you using this?
    Dim WithEvents Winsock As MSWinsockLib.Winsock
    Last edited by rory; Jun 2nd, 2006 at 06:54 PM.

  9. #9
    New Member
    Join Date
    Jun 2006
    Posts
    9

    Re: Winsock Question

    Does any of you guys know how to HTTP POST a local file to a web server using the MSXML component?

    Further details can be seen in this thread.
    http://www.vbforums.com/showthread.php?p=2520710

    Sincerely,
    Thomas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width