[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.
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:".
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.
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.
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:
Option Explicit
'Function to get the value of an HTTP variable.
'Used for parsing HTTP server responses.
Private Function GetHTTPVar(ByVal Name As String, ByVal HTTPData As String) As String
Dim lonPos As Long, lonLenName As Long
Dim strRet As String, lonLen As Long
Dim lonCrLf As Long
lonLenName = Len(Name)
lonLen = Len(HTTPData)
lonPos = InStr(1, HTTPData, Name, vbTextCompare)
If lonPos > 0 Then
lonCrLf = InStr(lonPos + 1, HTTPData, vbNewLine)
GetHTTPVar = Mid$(HTTPData, (lonPos + lonLenName), (lonCrLf - (lonPos + lonLenName)))
End If
End Function
Private Sub Form_Load()
Dim strHTTP As String
'Build some data to test with.
strHTTP = "HTTP/1.1 200 OK" & vbNewLine & _
"Date: Mon, 23 Oct 2006 12:29:35 GMT" & vbNewLine & _
"Server: Apache" & vbNewLine & _
"Expires: 0" & vbNewLine & _
"Cache-Control: private, post-check=0, pre-check=0, max-age=0" & vbNewLine & _
"Pragma: no-cache" & vbNewLine & _
"Content-Length: 111948" & vbNewLine & _
"Connection: Close" & vbNewLine & _
"Content-Type: text/html; charset=ISO-8859-1" & vbNewLine & vbNewLine
Dim strVar As String
'Get the Cache-Control variable.
strVar = GetHTTPVar("Cache-Control: ", strHTTP)
MsgBox strVar
End Sub
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.
Re: [RESOLVED] Sending HTTP requests
http://img146.imageshack.us/img146/7...1159oo1.th.png
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. . :D
Here is the complete Code of it:
VB Code:
'1 TextBox: name-> txtOut
'1 TextBox: name-> txtURL
'1 CommandButton: name-> cmdGo
Option Explicit
Private InetData As String
Private URL As String
Private Sub cmdGo_Click()
txtOut.Text = ""
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Connect URL, 80
End Sub
Private Sub Form_Load()
URL = txtURL.Text
End Sub
Private Sub txtURL_Change()
URL = txtURL.Text
End Sub
Private Sub Winsock1_Connect()
Call GetWebpage(Winsock1)
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData InetData
txtOut.Text = txtOut.Text & InetData
End Sub
Public Sub GetWebpage(wsk As Winsock)
With wsk
.SendData "GET /" & " HTTP/1.1" & vbCrLf
.SendData "Accept: text/plain" & vbCrLf
.SendData "Accept-Language: en-us" & vbCrLf
'.SendData "Accept-Encoding: gzip, deflate" & vbCrLf
'.SendData "User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)" & vbCrLf
.SendData "Host: " & wsk.LocalIP & vbCrLf
.SendData "Connection: Keep-Alive" & vbCrLf & vbCrLf
End With
End Sub
Re: [RESOLVED] Sending HTTP requests
Quote:
Originally Posted by DrGonzo
http://www.vbforums.com/
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. . :D
Here is the complete Code of it:
VB Code:
'1 TextBox: name-> txtOut
'1 TextBox: name-> txtURL
'1 CommandButton: name-> cmdGo
Option Explicit
Private InetData As String
Private URL As String
Private Sub cmdGo_Click()
txtOut.Text = ""
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Connect URL, 80
End Sub
Private Sub Form_Load()
URL = txtURL.Text
End Sub
Private Sub txtURL_Change()
URL = txtURL.Text
End Sub
Private Sub Winsock1_Connect()
Call GetWebpage(Winsock1)
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData InetData
txtOut.Text = txtOut.Text & InetData
End Sub
Public Sub GetWebpage(wsk As Winsock)
With wsk
.SendData "GET /" & " HTTP/1.1" & vbCrLf
.SendData "Accept: text/plain" & vbCrLf
.SendData "Accept-Language: en-us" & vbCrLf
'.SendData "Accept-Encoding: gzip, deflate" & vbCrLf
'.SendData "User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)" & vbCrLf
.SendData "Host: " & wsk.LocalIP & vbCrLf
.SendData "Connection: Keep-Alive" & vbCrLf & vbCrLf
End With
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:
strData = "GET /" & " HTTP/1.1" & vbCrLf
strData = strData & "Accept: text/plain" & vbCrLf
'etc..
.SendData strData
strData = ""