Results 1 to 8 of 8

Thread: [RESOLVED] Sending HTTP requests

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    101

    Resolved [RESOLVED] Sending HTTP requests

    GET http://www.vbforums.com/index.html HTTP/1.1

    Host: www.vbforums.com
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7
    Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
    Accept-Language: en-us,en;q=0.5
    Accept-Encoding: gzip,deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 300
    Connection: keep-alive

    How should I go about sending http requests etc with winsock? I know they need to include a header and a GET / POST request.

  2. #2
    Lively Member
    Join Date
    Oct 2006
    Posts
    81

    Re: Sending HTTP requests

    WEll you need to connect to the host (in this case www.vbforums.com) and send the request, dont forget about the blank lines (vbcrlf) they are important, in the request end usually there are 2 blank lines as i remember!
    And this: "Accept-Encoding: gzip,deflate" shows you will get the output data compressed with gzip/deflate, to recieve uncompressed text just do: "Accept-Encoding:".
    Last edited by Jean.B; Oct 23rd, 2006 at 03:13 AM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    101

    Re: Sending HTTP requests

    Ok I got it happening but when I get the response I'm not sure how to seperate the server's response, e.g.

    Recommend an exact RFC for me to read?

    HTTP/1.1 200 OK
    Date: Mon, 23 Oct 2006 12:29:35 GMT
    Server: Apache
    'PHPBBCOOKIE
    PHPBBCOOKIE
    PHPBBCOOKIE
    PHPBBCOOKIE
    Expires: 0
    Cache-Control: private, post-check=0, pre-check=0, max-age=0
    Pragma: no-cache
    Content-Length: 111948
    Connection: close
    Content-Type: text/html; charset=ISO-8859-1


    Thanks.

  4. #4
    Lively Member
    Join Date
    Oct 2006
    Posts
    81

    Re: Sending HTTP requests

    Well next you send the http request to the next page you want to go..collect cookies if they are sent, some sites checks checksum, if its wrong then it usually server sends you message back about wrong checksum.

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Sending HTTP requests

    Quote Originally Posted by skankerer
    Ok I got it happening but when I get the response I'm not sure how to seperate the server's response, e.g.

    Recommend an exact RFC for me to read?

    HTTP/1.1 200 OK
    Date: Mon, 23 Oct 2006 12:29:35 GMT
    Server: Apache
    'PHPBBCOOKIE
    PHPBBCOOKIE
    PHPBBCOOKIE
    PHPBBCOOKIE
    Expires: 0
    Cache-Control: private, post-check=0, pre-check=0, max-age=0
    Pragma: no-cache
    Content-Length: 111948
    Connection: close
    Content-Type: text/html; charset=ISO-8859-1


    Thanks.
    Do you mean parsing out individual things like content-length, cache-control, etc.? If so, you could use something like this:

    VB Code:
    1. Option Explicit
    2.  
    3. 'Function to get the value of an HTTP variable.
    4. 'Used for parsing HTTP server responses.
    5. Private Function GetHTTPVar(ByVal Name As String, ByVal HTTPData As String) As String
    6.     Dim lonPos As Long, lonLenName As Long
    7.     Dim strRet As String, lonLen As Long
    8.     Dim lonCrLf As Long
    9.    
    10.     lonLenName = Len(Name)
    11.     lonLen = Len(HTTPData)
    12.     lonPos = InStr(1, HTTPData, Name, vbTextCompare)
    13.    
    14.     If lonPos > 0 Then
    15.         lonCrLf = InStr(lonPos + 1, HTTPData, vbNewLine)
    16.         GetHTTPVar = Mid$(HTTPData, (lonPos + lonLenName), (lonCrLf - (lonPos + lonLenName)))
    17.     End If
    18.    
    19. End Function
    20.  
    21. Private Sub Form_Load()
    22.     Dim strHTTP As String
    23.    
    24.     'Build some data to test with.
    25.     strHTTP = "HTTP/1.1 200 OK" & vbNewLine & _
    26.         "Date: Mon, 23 Oct 2006 12:29:35 GMT" & vbNewLine & _
    27.         "Server: Apache" & vbNewLine & _
    28.         "Expires: 0" & vbNewLine & _
    29.         "Cache-Control: private, post-check=0, pre-check=0, max-age=0" & vbNewLine & _
    30.         "Pragma: no-cache" & vbNewLine & _
    31.         "Content-Length: 111948" & vbNewLine & _
    32.         "Connection: Close" & vbNewLine & _
    33.         "Content-Type: text/html; charset=ISO-8859-1" & vbNewLine & vbNewLine
    34.    
    35.    
    36.     Dim strVar As String
    37.    
    38.     'Get the Cache-Control variable.
    39.     strVar = GetHTTPVar("Cache-Control: ", strHTTP)
    40.     MsgBox strVar
    41. End Sub

  6. #6
    New Member
    Join Date
    Oct 2006
    Posts
    1

    Re: Sending HTTP requests

    Well if you want to send HTTP request to the server, the best idea would be XMLHTTP object. That would be simple to user other than going with winsock.

  7. #7
    New Member
    Join Date
    Nov 2006
    Posts
    9

    Talking Re: [RESOLVED] Sending HTTP requests



    Hello, I was also experimenting a little bit with HTTP and wrote this really little program.
    It only requests and receives a page which will be shown unformatted in a simple TextBox.

    What was your intention to it? Mine was a code-request in another forum, where someone wanted to log himself into a html-site by a vb-program.
    I suggested him to read this RFC: http://www.ietf.org/rfc/rfc2616.txt

    It would be nice if you could post your code as well. Just because I'm interested into comparing it etc. .

    Here is the complete Code of it:

    VB Code:
    1. '1 TextBox: name-> txtOut
    2. '1 TextBox: name-> txtURL
    3. '1 CommandButton: name-> cmdGo
    4.  
    5. Option Explicit
    6.  
    7. Private InetData As String
    8. Private URL As String
    9.  
    10. Private Sub cmdGo_Click()
    11. txtOut.Text = ""
    12. If Winsock1.State <> sckClosed Then Winsock1.Close
    13.     Winsock1.Connect URL, 80
    14. End Sub
    15.  
    16. Private Sub Form_Load()
    17.         URL = txtURL.Text
    18. End Sub
    19.  
    20. Private Sub txtURL_Change()
    21.     URL = txtURL.Text
    22. End Sub
    23.  
    24. Private Sub Winsock1_Connect()
    25.     Call GetWebpage(Winsock1)
    26.  
    27. End Sub
    28.  
    29. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    30.     Winsock1.GetData InetData
    31.     txtOut.Text = txtOut.Text & InetData
    32. End Sub
    33.  
    34. Public Sub GetWebpage(wsk As Winsock)
    35.  
    36. With wsk
    37.     .SendData "GET /" & " HTTP/1.1" & vbCrLf
    38.     .SendData "Accept: text/plain" & vbCrLf
    39.     .SendData "Accept-Language: en-us" & vbCrLf
    40.     '.SendData "Accept-Encoding: gzip, deflate" & vbCrLf
    41.     '.SendData "User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)" & vbCrLf
    42.     .SendData "Host: " & wsk.LocalIP & vbCrLf
    43.     .SendData "Connection: Keep-Alive" & vbCrLf & vbCrLf
    44. End With
    45.  
    46. End Sub

  8. #8
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: [RESOLVED] Sending HTTP requests

    Quote Originally Posted by DrGonzo


    Hello, I was also experimenting a little bit with HTTP and wrote this really little program.
    It only requests and receives a page which will be shown unformatted in a simple TextBox.

    What was your intention to it? Mine was a code-request in another forum, where someone wanted to log himself into a html-site by a vb-program.
    I suggested him to read this RFC: http://www.ietf.org/rfc/rfc2616.txt

    It would be nice if you could post your code as well. Just because I'm interested into comparing it etc. .

    Here is the complete Code of it:

    VB Code:
    1. '1 TextBox: name-> txtOut
    2. '1 TextBox: name-> txtURL
    3. '1 CommandButton: name-> cmdGo
    4.  
    5. Option Explicit
    6.  
    7. Private InetData As String
    8. Private URL As String
    9.  
    10. Private Sub cmdGo_Click()
    11. txtOut.Text = ""
    12. If Winsock1.State <> sckClosed Then Winsock1.Close
    13.     Winsock1.Connect URL, 80
    14. End Sub
    15.  
    16. Private Sub Form_Load()
    17.         URL = txtURL.Text
    18. End Sub
    19.  
    20. Private Sub txtURL_Change()
    21.     URL = txtURL.Text
    22. End Sub
    23.  
    24. Private Sub Winsock1_Connect()
    25.     Call GetWebpage(Winsock1)
    26.  
    27. End Sub
    28.  
    29. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    30.     Winsock1.GetData InetData
    31.     txtOut.Text = txtOut.Text & InetData
    32. End Sub
    33.  
    34. Public Sub GetWebpage(wsk As Winsock)
    35.  
    36. With wsk
    37.     .SendData "GET /" & " HTTP/1.1" & vbCrLf
    38.     .SendData "Accept: text/plain" & vbCrLf
    39.     .SendData "Accept-Language: en-us" & vbCrLf
    40.     '.SendData "Accept-Encoding: gzip, deflate" & vbCrLf
    41.     '.SendData "User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)" & vbCrLf
    42.     .SendData "Host: " & wsk.LocalIP & vbCrLf
    43.     .SendData "Connection: Keep-Alive" & vbCrLf & vbCrLf
    44. End With
    45.  
    46. End Sub
    I wouldn't use multiple .SendData methods like that.

    Instead, build all that data into a string variable, and send it off as one packet. ie:

    VB Code:
    1. strData = "GET /" & " HTTP/1.1" & vbCrLf
    2. strData = strData & "Accept: text/plain" & vbCrLf
    3. 'etc..
    4. .SendData strData
    5. strData = ""

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