tango.net.http.HttpClient

License:

BSD style: see license.txt

Version:

Initial release: April 2004 Outback release: December 2006

Author:

Kris - original module

Author:

h3r3tic - fixed a number of Post issues and bugs in the 'params' construction

Redirection handling guided via http://ppewww.ph.gla.ac.uk/~flavell/www/post-redirect.html
class 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 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.
alias void delegate(OutputBuffer) Pump [scope]
callback for sending PUT content
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(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.
HttpHeadersView getResponseHeaders()
Get the current input headers, as returned by the host request.
HttpHeaders getRequestHeaders()
Gain access to the request headers. Use this to add whatever headers are required for a request.
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.
UriView getUri()
Return the Uri associated with this client
ResponseLine getResponse()
Return the response-line for the latest request. This takes the form of "version status reason" as defined in the HTTP RFC.
int getStatus()
Return the HTTP status code set by the remote server
bool isResponseOK()
Return whether the response was OK or not
HttpClient addCookie(Cookie cookie)
Add a cookie to the outgoing headers
void close()
Close all resources used by a request. You must invoke this between successive open() calls.
HttpClient reset()
Reset the client such that it is ready for a new request.
HttpClient setRequest(RequestMethod method)
Set the request method
HttpClient setVersion(Version v)
Set the request version
HttpClient enableRedirect(bool yes = true)
enable/disable the internal redirection suppport
HttpClient setTimeout(float interval)
set timeout period for read operation
HttpClient keepAlive(bool yes = true)
Control keepalive option
HttpClient encodeUri(bool yes = true)
Control Uri output encoding
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.
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.
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.
Note that certain request-headers may generated automatically if they are not present. These include a Host header and, in the case of Post, both ContentType & ContentLength for a query type of request. The latter two are *not* produced for Post requests with 'pump' specified ~ when using 'pump' to output additional content, you must explicitly set your own headers.

Note also that IOException instances may be thrown. These should be caught by the client to ensure a close() operation is always performed
void read(scope void delegate(const(void)[]) sink, size_t len = max)
Read the content from the returning input stream, up to a maximum length, and pass content to the given sink delegate as it arrives.
Exits when length bytes have been processed, or an Eof is seen on the stream.
InputBuffer redirectPost(Pump pump, int status)
Handle redirection of Post Guidance for the default behaviour came from this page: http://ppewww.ph.gla.ac.uk/~flavell/www/post-redirect.html
bool canRepost(uint status)
Handle user-notification of Post redirection. This should be overridden appropriately.
Guidance for the default behaviour came from this page: http://ppewww.ph.gla.ac.uk/~flavell/www/post-redirect.html
Socket createSocket() [protected]
Overridable socket factory, for use with HTTPS and so on