alice
library
manual.

Alice Project

The HttpClient structure


________ Synopsis ____________________________________________________

    signature HTTP_CLIENT
    structure HttpClient : HTTP_CLIENT

This structure implements a simple client for the HTTP protocol as specified in RFC 2616. Connections are only created and maintained open for issuing a single request and reading a single response.

Where a request is constructed, the current implementation inserts a User-Agent of Alice/1.0 and uses protocol version 1.1.

See also: Url, Http, HttpServer, Resolver, Socket


________ Import ______________________________________________________

    import structure HttpClient from "x-alice:/lib/system/HttpClient"
    import signature HTTP_CLIENT from "x-alice:/lib/system/HTTP_CLIENT-sig"

________ Interface ___________________________________________________

    signature HTTP_CLIENT =
    sig
	type document = {contentType : string, body : string}

	exception Authority

	val request : Url.t * Http.request -> Http.response
	val get : Url.t -> Http.response
	val post : Url.t * document -> Http.response
    end

________ Description _________________________________________________

type document = {contentType : string, body : string}

The type of documents as provided in a POST request.

exception Authority

indicates that a given URL did either not contain an authority or that it was not well-formed (for instance, a port number was supplied, but it was no valid integer).

request (url, request)

establishes a connection to the server specified in url, issues the request, and returns the response. Closes the connection immediately after reading the response. Raises Authority if url does not specify a well-formed authority.

get url

establishes a connection to the server specified in url, issues a GET request, and returns the response. Closes the connection immediately after reading the response. Raises Authority if url does not specify a well-formed authority. Raises IO.Io if an error occurs during communication.

post (url, doc)

establishes a connection to the server specified in url, issues a POST request with doc, and returns the response. Closes the connection immediately after reading the response. Raises Authority if url does not specify a well-formed authority. Raises IO.Io if an error occurs during communication.


________ Examples ____________________________________________________

The following example implements a simple stand-alone application that takes a URL on its command line, issues a corresponding GET request, and dumps the response status and headers to TextIO.stdErr and the document to TextIO.stdOut.

Download full source code

fun usage () =
    TextIO.output (TextIO.stdErr,
		   "Usage: " ^ CommandLine.name () ^ " \n")

fun main [url] =
    (let
	val response = HttpClient.get (Url.fromString url)
    in
	TextIO.output
	    (TextIO.stdErr, Int.toString (#statusCode response) ^ " " ^
			    #reasonPhrase response ^ "\n");
	Http.StringMap.appi
	    (fn (name, value) =>
		TextIO.output (TextIO.stdErr, name ^ ": " ^ value ^ "\n"))
	    (#headers response);
	TextIO.output (TextIO.stdErr, "\n");
	TextIO.print (#body response);
	OS.Process.success
    end
    handle IO.Io {cause, ...} =>
	(TextIO.output (TextIO.stdErr,
			case cause of Http.Format => "unsupported HTTP format\n"
				    | e => "I/O error " ^ Exn.name e ^ "\n");
	 OS.Process.failure))
  | main _ = (usage (); OS.Process.failure)

val _ = OS.Process.exit (main (CommandLine.arguments ()))


last modified 2007/Mar/30 17:10