The HTTP library allows access to Internet resources using the HTTP protocol.
Communication is done with 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 required 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.
As of version 1.5 it is possible to make asynchronous requests using
WebRequest.GetAsyncResponse, and it is possible to retrieve the data asynchronously
with WebResponse.GetAsyncStream or WebResponse.GetAsyncString.
Unlike the synchronous calls the asynchronous calls will not freeze the interface during
communication.
This help manual doesn't cover the HTTP 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
dim Buffer(0) as byte
End Sub
Sub App_Start
Form1.Show
URL = "http://www.basic4ppc.com/forum/images/Basic4ppc2.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