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 structure HttpClient from "x-alice:/lib/system/HttpClient"
import signature HTTP_CLIENT from "x-alice:/lib/system/HTTP_CLIENT-sig"
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
The type of documents as provided in a POST request.
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).
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.
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.
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.
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.
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 ()))