Overview (HTTP)
Accept (WebRequest)
ConnectionLength (WebRequest)
ConnectionType (WebRequest)
GetResponse (WebRequest)
GetStream (WebRequest)
Headers (WebRequest)
KeepAlive (WebRequest)
Method (WebRequest)
New1 (WebRequest)
New2 (WebRequest)
SetProxy (WebRequest)
TimeOut (WebRequest)
Close (WebResponse)
ContentLength (WebResponse)
ContentType (WebResponse)
GetStream (WebResponse)
GetString (WebResponse)
Headers (WebResponse)
New1 (WebResponse)
New2 (WebResponse)
Value (WebResponse)
Overview (HTTP) Top
The HTTP library allows access to Internet resources using the HTTP protocol.
The communication is done using two objects; WebRequest which makes the request and WebResponse which includes the data received from the server (like a html file).
The stages of communicating with a HTTP server are:
- Create a WebRequest object with the matching URL.
- Set the WebRequest header properties.
- If you need to upload data with the request use WebRequest.GetStream and write to the stream.
- Launch the request and assign the response to a WebResult object.
The last operation blocks until the data is received.
- Use the WebResult.GetStream to get the data stream and read the result from it.
- Close the WebResult.
To write to the WebRequest stream or read from the WebResponse stream you need to use the updated BinaryFile (version 1.2).
This help manual doesn't cover the HTTP protocol.
The HTTP protocol specifications can be found here: http://www.w3.org/Protocols/rfc2616/rfc2616.html
Another useful resource: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
The following code is an example of some usages of the HTTP library:
'Request is a WebRequest object, Response is a WebResponse object
'Reader and Writer are BinaryFile objects.
Sub Globals
End Sub
Sub App_Start
Form1.Show
URL = "http://b4ppcforum.geotrail.no/styles/subSilver/imageset/Baisc4ppc.gif"
DownloadFile(AppPath & "\NewImage.gif",URL)
Form1.Image = "NewImage.gif"
URL = "http://en.wikipedia.org/wiki/HTTP"
TextBox1.Text = GetText(URL) 'TextBox1 is a multiline textbox.
End Sub
Sub GetText (URL)
Response.New1
Request.New1(URL)
Response.Value = Request.GetResponse 'This line calls the server and gets the response.
string = Response.GetString 'Get the Response string.
Response.Close
return string
End Sub
Sub UploadFile (UploadFile, URL) 'Requires a server with write permission.
Response.New1
Request.New1(URL)
Request.Method = "PUT"
Writer.New1(Request.GetStream,true) 'Use a BinaryFile object to write the data to the Request stream.
dim Buffer(4096) as byte
FileOpen(c1,UploadFile,cRandom)
Reader.New1(c1,true) 'Reads the local file.
count = Reader.ReadBytes(buffer(),4096)
Do While count > 0
Writer.WriteBytes2(buffer(),0,count)
count = Reader.ReadBytes(buffer(),4096)
loop
FileClose(c1)
Request.GetResponse 'Launch the request (upload the file).
End Sub
Sub DownloadFile (LocalFile,URL)
Response.New1
Request.New1(URL)
Response.Value = Request.GetResponse 'Launch the request and get the response.
Msgbox("Download size: " & Response.ContentLength) 'Show the file size.
Reader.New1(Response.GetStream,true) 'Use a BinaryFile object to read the data from the Response stream.
FileOpen(c1,LocalFile,cRandom)
Writer.New1(c1,false)
dim buffer(4096) as byte
count = Reader.ReadBytes(buffer(),4096)
do while count > 0
Writer.WriteBytes2(buffer(),0,count)
count = Reader.ReadBytes(buffer(),4096)
loop
FileClose(c1)
Response.Close 'Close the Response stream.
Msgbox("File saved.")
End Sub
Accept (WebRequest) Top
Gets or sets the value of the Accept HTTP header (String).
Syntax: Accept
ConnectionLength (WebRequest) Top
Gets or sets the value of the Content-length HTTP header (Int64).
Syntax: ConnectionLength
ConnectionType (WebRequest) Top
Gets or sets the value of the Content-type HTTP header (String).
Syntax: ConnectionType
GetResponse (WebRequest) Top
Sends the request to the server and returns the response.
This method blocks until the data is received (or the TimeOut limit).
Syntax: GetResponse
Example:
Response.New1
Request.New1("www.basic4ppc.com")
Response.Value = Request.GetResponse 'This line calls the server and gets the response.
TextBox1.Text = Response.GetString 'Gets the Response string.
Response.Close
GetStream (WebRequest) Top
Gets the request stream.
Using this stream with a BinaryFile object you can write data that will be sent to the server.
Syntax: GetStream
The stream will be closed when the GetResponse method is called.
Example:
Response.New1
Request.New1(URL)
Request.Method = "PUT"
Writer.New1(Request.GetStream,true) 'Use a BinaryFile object to write the data to the Request stream.
dim Buffer(4096) as byte
FileOpen(c1,UploadFile,cRandom)
Reader.New1(c1,true) 'Reads the local file.
count = Reader.ReadBytes(buffer(),4096)
Do While count > 0
Writer.WriteBytes2(buffer(),0,count)
count = Reader.ReadBytes(buffer(),4096)
loop
FileClose(c1)
Request.GetResponse 'Launch the request (upload the file).
Headers (WebRequest) Top
Gets a string representing the request headers.
Syntax: Headers
KeepAlive (WebRequest) Top
Gets or sets a value whether to make a persistent connection (Boolean value).
Syntax: KeepAlive
The default is true.
Method (WebRequest) Top
Gets or sets the HTTP method.
Syntax: Method
The value can be: GET, HEAD, POST, PUT, DELETE, TRACE or OPTIONS.
The default value is GET.
Example:
Request.Method = "HEAD"
New1 (WebRequest) Top
Initializes a WebRequest object using the specified URL.
Syntax: New1 (URL As String)
The default encoding is used: UTF-8.
Example:
Request.New1("http://www.basic4ppc.com")
New2 (WebRequest) Top
Initializes a WebRequest using the specified URL and using the specifed code page encoding.
The code page values can be found here: http://msdn2.microsoft.com/en- us/library/ms776446.aspx
Syntax: New2 (URL As String, CodePage As Int32)
Example:
Request.New2 ("http://www.basic4ppc.com",20127) 'ASCII encoding
SetProxy (WebRequest) Top
Causes all requests to use a proxy server.
Syntax: SetProxy (Host As String, PortNumber As Int32, BypassOnLocal As Boolean)
Host - The name of the proxy host.
PortNumber - The port to use.
BypassOnLocal - Whether to bypass the proxy on local addresses.
TimeOut (WebRequest) Top
Gets or sets the timeout value when calling GetResponse.
The value is measured using milliseconds.
Syntax: TimeOut
The default value is 10000 (10 seconds).
Example:
Request.TimeOut = 50000 '50 seconds
Close (WebResponse) Top
Closes the response stream.
Syntax: Close
Example:
Response.Close
ContentLength (WebResponse) Top
Gets the length (number of byte) of the content returned from the server.
Syntax: ContentLength
Example:
Response.New1
Request.New1(URL)
Response.Value = Request.GetResponse 'Launch the request and get the response.
Msgbox("Download size: " & Response.ContentLength) 'Show the file size.
ContentType (WebResponse) Top
Gets the value of the Content-Type HTTP header returned from the server.
Syntax: ContentType
GetStream (WebResponse) Top
Gets a stream containing the data received from the server.
Using a BinaryFile object you can read the data from the stream.
Syntax: GetStream
Example:
Reader.New1(Response.GetStream,true) 'Reader is a BinaryFile object
GetString (WebResponse) Top
Gets a string from the response returned from the server.
GetString is useful when fetching a text resource (like a html page).
Syntax: GetString
Example:
TextBox1.Text = Response.GetString
Response.Close
Headers (WebResponse) Top
Gets a string representing the response headers.
Syntax: Headers
New1 (WebResponse) Top
Initializes a WebResponse object.
The encoding used is UTF-8.
Syntax: New1
New2 (WebResponse) Top
Initializes a WebResponse object using the specified encoding.
The code page values can be found here: http://msdn2.microsoft.com/en- us/library/ms776446.aspx
Syntax: New2 (CodePage As Int32)
Value (WebResponse) Top
Sets the value of a WebResponse object.
Syntax: Value
Example:
Response.Value = Request.GetResponse