A client that receives content, such as web pages, from a server using the HTTP protocol.
HttpClient contains a number of methods to send an HttpClientRequest to an Http server and receive an HttpClientResponse back. For example, you can use the get, getUrl, post, and postUrl methods for GET and POST requests, respectively.
A getUrl
request is a two-step process, triggered by two Futures.
When the first future completes with a HttpClientRequest, the underlying
network connection has been established, but no data has been sent.
In the callback function for the first future, the HTTP headers and body
can be set on the request. Either the first write to the request object
or a call to close sends the request to the server.
When the HTTP response is received from the server, the second future, which is returned by close, completes with an HttpClientResponse object. This object provides access to the headers and body of the response. The body is available as a stream implemented by HttpClientResponse. If a body is present, it must be read. Otherwise, it leads to resource leaks. Consider using HttpClientResponse.drain if the body is unused.
HttpClient client = new HttpClient();
client.getUrl(Uri.parse("http://www.example.com/"))
.then((HttpClientRequest request) {
// Optionally set up headers...
// Optionally write to the request object...
// Then call close.
...
return request.close();
})
.then((HttpClientResponse response) {
// Process the response.
...
});
The future for HttpClientRequest is created by methods such as getUrl and open.
An HttpClient can make HTTPS requests, connecting to a server using the TLS (SSL) secure networking protocol. Calling getUrl with an https: scheme will work automatically, if the server's certificate is signed by a root CA (certificate authority) on the default list of well-known trusted CAs, compiled by Mozilla.
To add a custom trusted certificate authority, or to send a client
certificate to servers that request one, pass a SecurityContext object
as the optional context
argument to the HttpClient
constructor.
The desired security options can be set on the SecurityContext object.
All HttpClient requests set the following header by default:
Accept-Encoding: gzip
This allows the HTTP server to use gzip compression for the body if
possible. If this behavior is not desired set the
Accept-Encoding
header to something else.
To turn off gzip compression of the response, clear this header:
request.headers.removeAll(HttpHeaders.acceptEncodingHeader)
The HttpClient supports persistent connections and caches network connections to reuse them for multiple requests whenever possible. This means that network connections can be kept open for some time after a request has completed. Use HttpClient.close to force the HttpClient object to shut down and to close the idle network connections.
By default the HttpClient uses the proxy configuration available
from the environment, see findProxyFromEnvironment. To turn off
the use of proxies set the findProxy property to
null
.
HttpClient client = new HttpClient();
client.findProxy = null;
url
and realm
. [...]
host
,
port
and realm
. [...]
url
. If this
function is not set, direct connections will always be used. [...]
User-Agent
header for all requests
generated by this HttpClient. [...]
defaultHttpPort
defaultHttpsPort
80
443