PocketHTTP

Jump to Downloads | Online Documentation

This is an Open Source [MPL] HTTP/1.1 client COM component for the Windows family (PocketPC/95/98/Me/NT4/2000/XP/2003), originally based on HTTP transport from PocketSOAP.

PocketHTTP includes the following features
Latest Version : PocketHTTP v1.3.4, released Saturday, September 15, 2012

Easy to use

' do a HTTP GET from http://www.pocketsoap.com
set req = CreateObject("pocket.HTTP")
set res = req.GetResponse("http://www.pocketsoap.com/", "")
' show the response as a string
wscript.echo res.String
' what was the status code returned ?
wscript.echo res.StatusCode
' what was the Content-Type header set to ?
wscript.echo res.Headers.Find("Content-Type").Value

' generates this request
GET / HTTP/1.1
Host: www.pocketsoap.com
Accept-Encoding: gzip, deflate
User-Agent: PocketHTTP/1.2.5
' do a HTTP PUT to www.pocketsoap.com/store/1
set req = CreateObject("pocket.HTTP")
req.method = "PUT"
req.Headers.Create "Content-Type", "text/xml"
set res = req.GetResponse("http://www.pocketsoap.com/store/1", "<root>Hello World!</root>" )
wscript.echo res.statusCode

' generates this HTTP request
PUT /store/1 HTTP/1.1
Host: www.pocketsoap.com
Accept-Encoding: gzip, deflate
Content-Type: text/xml
User-Agent: PocketHTTP/1.2.6
Content-Length: 25

<root>Hello World!</root>
' get a GIF and write it direct to disk
set req = CreateObject("Pocket.HTTP")
set res = req.getResponse("http://www.pocketsoap.com/logo.gif", "")
res.SaveAs "c:\logo.gif"

How To Use

PocketHTTP is easy to use, it just consists of two objects, the request object and the response object. Create and configure a request object then use it to make a HTTP request, and get the resulting response object.

Basics

set req = CreateObject("pocket.HTTP")
set res = req.GetResponse("http://www.pocketsoap.com/","")
Use the Method property on the request object to change the HTTP Verb used in the request, this defaults to GET, e.g. to change it to POST do
req.method = "POST"
Use the Headers collection property to manipulate the HTTP headers sent in the request, for example to set a content-type header for the above POST, you would use the create method to create a new header, e.g.
req.headers.create "Content-Type", "text/xml"
To alter the existing User-Agent header, you use the Find method to find the existing header and modify its value, e.g.
req.headers.find("User-Agent").Value = "Simon's Super Slurper/0.42"
For HTTP requests that include a body (such as POST or PUT), the 2nd parameter to the GetResponse method is used to pass this data. It can be either a string (which is automatically transcoded to utf-8), an array of bytes, or a stream (IStream or IResetableStream), e.g.
set res = req.GetResponse("http://www.pocketsoap.com/store/1", "<root>Hello Again!</root>")

Proxy Servers

If you want the request to go via a HTTP proxy server, use the SetProxy method to indicate the name (or IP address) and port where the proxy server is running, e.g.
req.setProxy "proxy.corp.com", 8080
If the proxy requires authentication, use the ProxyAuthentication method to set the proxy authentication credenitals, e.g.
req.ProxyAuthentication "myProxyAccount", "yeahright"
To cancel a previously set proxy configuration, use the NoProxy method, e.g.
req.NoProxy

Authentication

To set authentication credentials for the target server, use the authentication method
req.authentication "myServerAccount", "foo"

Timeouts

To alter the timeout from its default of 15 seconds, alter the timeout property (this is in milliseconds), e.g.
req.timeout = 5000

Working with the Response

Once the request object is configured as required, use the GetResponse method to make a request to a particular server and get the response object. You can call GetResponse multiple times to re-use the same configuration to make multiple requests. The response object can return the response data in 3 different formats, as a String using the String method, as an array of bytes using the Bytes method or as a stream using the Stream method, e.g.
set req = CreateObject("Pocket.HTTP")
set res = req.GetResponse("http://www.pocketsoap.com/")
wscript.echo res.string
Note that the 3 methods are alternatives, for a particular response you should use just one of them, and use it just once (the component does not copy the response data).
The response object also contains a headers collection property to allow you to examine the response headers, e.g.
wscript.echo res.headers.find("Content-Type").Value
The headers collection also supports a standard COM enumerator, so that you can examine all the headers in the collection, e.g.
for each h in res.Headers
	wscript.echo h.Name & ": " & h.Value
next
The response object also includes a statusCode property, so that you can examine the value of the HTTP status code set by the server, e.g.
wscript.echo res.statusCode
For diagnostic purposes you can ask it to keep a disk log of the request & response messages, e.g.
req.option("tracing.file") = "c:\http.log"
If you have a large file that you want to PUT or POST, then to make that easier there a ReaderFactory class that will create a stream for you from the file, you can then pass this stream object as the parameter to GetResponse.
Dim f As New PocketHTTP.CoFileReaderFactory
Dim stream As IUnknown
Set stream = fac.CreateReaderFromFile("c:\myLargeFile.bin")
req.Method = "PUT"
set res = req.GetResponse("http://my.server.org/files/myLargeFile.bin", stream);

Compression

by default PocketHTTP will send an Accept-Encoding: gzip, deflate header indicating that it can handled a compressed response, if the server returns a compressed response PocketHTTP will automatically un-compress it for you. You can use the following option values to alter the compression settings. e.g, to PUT a file and have the file contents be compressed, you'd do
Dim f As New PocketHTTP.CoFileReaderFactory
Dim stream As IUnknown
Set stream = fac.CreateReaderFromFile("c:\myLargeFile.bin")
req.Method = "PUT"
req.option("compress.enabled") = true
set res = req.GetResponse("http://my.server.org/files/myLargeFile.bin", stream);

Cookies

You can access the internal cookies collection that each request object maintains, e.g.
set req = createObject("Pocket.HTTP")
set res = req.GetResponse("http://www.pocketsoap.com/", "")
wscript.echo "html body is "  & len(res.String) & " bytes long"

set cc = req.option("cookies")
for each c in cc
	wscript.echo c.domain & " : " & c.path & " : " & c.name & " -> " & c.value
next
You can copy cookies from one request object to another, e.g.
' req is an existing request you want to copy the cookies from
set cc = req.option("cookies")
set req2 = createObject("Pocket.HTTP")
req2.option("cookies").copy(cc)
You can create bake new cookies from scratch, e.g.
set req = createObject("Pocket.HTTP")
set cc = req.option("cookies")
cc.SetCookie "simon", "foo", "/", "www.pocketsoap.com"

Binaries & Source

Pre packaged Binaries and source are available under the Mozila Public License v1.1 (MPL)

Version Size (Kb) Platform Notes
Win32 Version v1.3.4 Packaged Install 151 supports Windows 95 OSR2 / 98 / Me / NT4 / 2000 / XP / 2003 Windows 95 requires DCOM95 Installing first
Windows 95/98 requires additional installs for SSL support, see this KB article for details.
PocketPC Version v1.3.4 Packaged Install 355 supports ARM, SH3 & MIPS running PocketPC and ARM devices running PocketPC, PocketPC 2002, PocketPC 2003 and Windows Mobile 5.0 requires ActiveSync 3.0 or greater to install, tested on Compaq iPaq H3650 (ARM) with a CF ethernet card.
v1.3.4 Source Code 1876 Single source tree for both PocketPC & desktop versions Read the building the source notes first !
Source code is also available via the SVN repository at google code.
v1.3.4 Binaries for the PocketPC 2000 emulator 74 Built binaries suitable for installation into the pocketPC 2000 emulator. Run regsvrce on pocketHTTP.dll
v1.3.4 Binaries for the PocketPC 2002 emulator 74 Built binaries suitable for installation into the pocketPC 2002 emulator. Run regsvrce on pocketHTTP.dll

Release History

Version 1.3.4, Sept 15, 2012 Version 1.3.3, July 13, 2008 Version 1.3.2, Jan 27, 2007 Version 1.3.1, Dec 24, 2006 Version 1.3.0, July 17, 2006 Version 1.2.7, May 2, 2006 Version 1.2.6, May 14, 2005 Version 1.2.5, April 23, 2005 Version 1.2.3, February 23, 2005 Version 1.2.2, September 18, 2004 Version 1.2.1, July 17, 2004 Version 1.2, March 27, 2004 Version 1.1, February 1, 2004 Version 1.0.2, May 10, 2003 Version 1.0.1, April 26, 2003 Version 1.0.0, March 1, 2003