HttpClient

Supports the basic needs of a client making requests of an HTTP server. The following is an example of how this might be used:

// callback for client reader
void sink (void[] content)
{
        Stdout (cast(char[]) content);
}

// create client for a GET request
auto client = new HttpClient (HttpClient.Get, "http://www.yahoo.com");

// make request
client.open;

// check return status for validity
if (client.isResponseOK)
   {
   // extract content length
   auto length = client.getResponseHeaders.getInt (HttpHeader.ContentLength);

   // display all returned headers
   Stdout (client.getResponseHeaders);

   // display remaining content
   client.read (&sink, length);
   }
else
   Stderr (client.getResponse);

client.close();

See modules HttpGet and HttpPost for simple wrappers instead.

Constructors

this
this(RequestMethod method, const(char)[] url)

Create a client for the given URL. The argument should be fully qualified with an "http:" or "https:" scheme, or an explicit port should be provided.

this
this(RequestMethod method, Uri uri)

Create a client with the provided Uri instance. The Uri should be fully qualified with an "http:" or "https:" scheme, or an explicit port should be provided.

Members

Aliases

Pump
alias Pump = void delegate(OutputBuffer)

callback for sending PUT content

Enums

Version
enum Version
Undocumented in source.

Functions

addCookie
HttpClient addCookie(Cookie cookie)

Add a cookie to the outgoing headers

canRepost
bool canRepost(uint status)

Handle user-notification of Post redirection. This should be overridden appropriately.

close
void close()

Close all resources used by a request. You must invoke this between successive open() calls.

createSocket
Socket createSocket()

Overridable socket factory, for use with HTTPS and so on

enableRedirect
HttpClient enableRedirect(bool yes)

enable/disable the internal redirection suppport

encodeUri
HttpClient encodeUri(bool yes)

Control Uri output encoding

getRequestHeaders
HttpHeaders getRequestHeaders()

Gain access to the request headers. Use this to add whatever headers are required for a request.

getRequestParams
HttpParams getRequestParams()

Gain access to the request parameters. Use this to add x=y style parameters to the request. These will be appended to the request assuming the original Uri does not contain any of its own.

getResponse
ResponseLine getResponse()

Return the response-line for the latest request. This takes the form of "version status reason" as defined in the HTTP RFC.

getResponseHeaders
HttpHeadersView getResponseHeaders()

Get the current input headers, as returned by the host request.

getStatus
int getStatus()

Return the HTTP status code set by the remote server

getUri
UriView getUri()

Return the Uri associated with this client

isResponseOK
bool isResponseOK()

Return whether the response was OK or not

keepAlive
HttpClient keepAlive(bool yes)

Control keepalive option

open
InputBuffer open()

Make a request for the resource specified via the constructor, using the specified timeout period (in milli-seconds).The return value represents the input buffer, from which all returned headers and content may be accessed.

open
InputBuffer open(Pump pump)

Make a request for the resource specified via the constructor, using a callback for pumping additional data to the host. This defaults to a three-second timeout period. The return value represents the input buffer, from which all returned headers and content may be accessed.

open
InputBuffer open(RequestMethod method, Pump pump)

Make a request for the resource specified via the constructor using the specified timeout period (in micro-seconds), and a user-defined callback for pumping additional data to the host. The callback would be used when uploading data during a 'put' operation (or equivalent). The return value represents the input buffer, from which all returned headers and content may be accessed.

read
void read(void delegate(const(void)[]) sink, size_t len)

Read the content from the returning input stream, up to a maximum length, and pass content to the given sink delegate as it arrives.

redirectPost
InputBuffer redirectPost(Pump pump, int status)

Handle redirection of Post

reset
HttpClient reset()

Reset the client such that it is ready for a new request.

setRequest
HttpClient setRequest(RequestMethod method)

Set the request method

setTimeout
HttpClient setTimeout(float interval)

set timeout period for read operation

setVersion
HttpClient setVersion(Version v)

Set the request version

Static variables

Connect
RequestMethod Connect;
Delete
RequestMethod Delete;
Undocumented in source.
Get
RequestMethod Get;
Head
RequestMethod Head;
Options
RequestMethod Options;
Post
RequestMethod Post;
Put
RequestMethod Put;
Trace
RequestMethod Trace;
Undocumented in source.

Meta