blob: 48c3e3723a5c4342b627b164a194ff517cf6904c [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>API Usage</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty : The Definitive Reference"><link rel="up" href="http-client.html" title="Chapter&nbsp;27.&nbsp;HTTP Client"><link rel="prev" href="http-client.html" title="Chapter&nbsp;27.&nbsp;HTTP Client"><link rel="next" href="http-client-other.html" title="Other Features"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><script type="text/javascript" src="js/shCore.js"></script><script type="text/javascript" src="js/shBrushJava.js"></script><script type="text/javascript" src="js/shBrushXml.js"></script><script type="text/javascript" src="js/shBrushBash.js"></script><script type="text/javascript" src="js/shBrushJScript.js"></script><script type="text/javascript" src="js/shBrushSql.js"></script><script type="text/javascript" src="js/shBrushProperties.js"></script><script type="text/javascript" src="js/shBrushPlain.js"></script><link type="text/css" rel="stylesheet" href="css/shCore.css"><link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"><link type="text/css" rel="stylesheet" href="css/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small">
Version: 9.3.0.v20150612</span></td><td style="width: 50%"><script type="text/javascript"> (function() {
var cx = '016459005284625897022:obd4lsai2ds';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script><gcse:search></gcse:search></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">API Usage</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="http-client.html"><i class="icon-chevron-left"></i> Previous</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;27.&nbsp;HTTP Client<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="http-client-other.html">Next <i class="icon-chevron-right"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/">Contact the core Jetty developers at
<span class="website">www.webtide.com</span></a></h5><p>
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ...
scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="http-client-api"></a>API Usage</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="http-client-api.html#http-client-blocking">Blocking APIs</a></span></dt><dt><span class="section"><a href="http-client-api.html#http-client-async">Asynchronous APIs</a></span></dt><dt><span class="section"><a href="http-client-api.html#http-client-content">Content Handling</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="http-client-blocking"></a>Blocking APIs</h3></div></div></div><p>The simpler way to perform a HTTP request is the following:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
ContentResponse response = httpClient.GET("http://domain.com/path?query");
]]>
</script></div><p>Method <code class="code">HttpClient.GET(...)</code> performs a HTTP GET request to the given URI and returns a
<code class="code">ContentResponse</code> when the request/response conversation completes successfully.</p><p>The <code class="code">ContentResponse</code> object contains the HTTP response information: status code, headers
and possibly a content. The content length is limited by default to 2 MiB; for larger content see
<a class="xref" href="http-client-api.html#http-client-response-content" title="Response Content Handling">Response Content Handling</a>.</p><p>If you want to customize the request, for example by issuing a HEAD request instead of a GET, and simulating
a browser user agent, you can do it in this way:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
ContentResponse response = httpClient.newRequest("http://domain.com/path?query")
.method(HttpMethod.HEAD)
.agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0")
.send();
]]>
</script></div><p>This is a shorthand for:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
Request request = httpClient.newRequest("http://domain.com/path?query");
request.method(HttpMethod.HEAD);
request.agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0");
ContentResponse response = request.send();
]]>
</script></div><p>You first create a request object using <code class="code">httpClient.newRequest(...)</code>, and then you customize it
using the fluent API style (that is, chained invocation of methods on the request object). When the request object
is customized, you call <code class="code">Request.send()</code> that produces the <code class="code">ContentResponse</code> when
the request/response conversation is complete.</p><p>Simple POST requests also have a shortcut method:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
ContentResponse response = httpClient.POST("http://domain.com/entity/1")
.param("p", "value")
.send();
]]>
</script></div><p>The POST parameter values are automatically URL-encoded.</p><p>Jetty HTTP client automatically follows redirects, so automatically handles the typical web pattern <a class="link" href="http://en.wikipedia.org/wiki/Post/Redirect/Get" target="_top">POST/Redirect/GET</a>, and the response object contains
the content of the response of the GET request. Following redirects is a feature that you can enable/disable on a
per-request basis or globally.</p><p>File uploads also require one line, and make use of JDK 7&#8242;s <code class="code">java.nio.file</code> classes:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
ContentResponse response = httpClient.newRequest("http://domain.com/upload")
.file(Paths.get("file_to_upload.txt"), "text/plain")
.send();
]]>
</script></div><p>It is possible to impose a total timeout for the request/response conversation using the
<code class="code">Request.timeout(...)</code> method, in this way:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
ContentResponse response = httpClient.newRequest("http://domain.com/path?query")
.timeout(5, TimeUnit.SECONDS)
.send();
]]>
</script></div><p>In the example above, when the 5 seconds expire, the request is aborted and a
<code class="classname">java.util.concurrent.TimeoutException</code> is thrown.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="http-client-async"></a>Asynchronous APIs</h3></div></div></div><p>So far we have shown how to use Jetty HTTP client in a blocking style, that is the thread that issues the
request blocks until the request/response conversation is complete. In this section we will look at Jetty HTTP
client asynchronous, non-blocking, APIs that are perfectly suited for large content downloads, for parallel
processing of requests/responses and in all those cases where performance and efficient thread and resource
utilization is a key factor.</p><p>The asynchronous APIs rely heavily on listeners that are invoked at various staged of request and response
processing. These listeners are implemented by applications and may perform any kind of logic.
The implementation invokes these listeners in the same thread that is used to process the request or response.
Therefore, if the application code in these listeners takes a long time to execute, the request or response
processing is delayed until the listener returns.</p><p>If you need to execute application code that takes long time inside a listener, you must spawn your own
thread, and remember to deep copy any data provided by the listener that you will need in your code, because
when the listener returns the data it provides may be recycled/cleared/destroyed.</p><p>Request and response processing are executed by two different threads and therefore may happen concurrently.
A typical example of this concurrent processing is an echo server, where a large upload may be concurrent with
the large download echoed back. As a side note, remember that responses may be processed and completed
<span class="emphasis"><em>before</em></span> requests; a typical example is a large upload that triggers a quick response - for
example an error - by the server: the response may arrive and be completed while the request content is still
being uploaded.</p><p>The application thread that calls <code class="code">Request.send(CompleteListener)</code> performs the processing
of the request until either the request is fully processed or until it would block on I/O, then it returns (and
therefore never blocks). If it would block on I/O, the thread asks the I/O system to emit an event when the
I/O will be ready to continue, then returns. When such an event is fired, a thread taken from the
<code class="code">HttpClient</code> thread pool will resume the processing of the request.</p><p>Response are processed either from the I/O system thread that fires the event that bytes are ready to
be read or by a thread taken from the <code class="code">HttpClient</code> thread pool (this is controlled by the
<code class="code">HttpClient.isDispatchIO()</code> property). Response processing continues until either the response is
fully processed or until it would block for I/O. If it would block for I/O, the thread asks the I/O system to
emit an event when the I/O will be ready to continue, then returns. When such an event is fired, a thread taken
from the <code class="code">HttpClient</code> thread pool will resume the processing of the response.</p><p>When the request and the response are both fully processed, the thread that finished the last processing
(usually the thread that processes the response, but may also be the thread that processes the request - if the
request takes more time than the response to be processed) is used to dequeue the next request for the same
destination and processes it.</p><p>A simple asynchronous GET request that discards the response content can be written in this way:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
httpClient.newRequest("http://domain.com/path")
.send(new Response.CompleteListener()
{
@Override
public void onComplete(Result result)
{
// Your logic here
}
});
]]>
</script></div><p>Method <code class="code">Request.send(Response.CompleteListener)</code> returns void and does not block; the <code class="code">
Response.CompleteListener</code> provided as a parameter is notified when the request/response conversation is
complete, and the <code class="code">Result</code> parameter allows you to access the response object.</p><p>You can write the same code using JDK 8&#8242;s lambda expressions:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
httpClient.newRequest("http://domain.com/path")
.send((result) -> { /* Your logic here */ });
]]>
</script></div><p>You can impose a total timeout for the request/response conversation in the same way used by the
synchronous API:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
Request request = httpClient.newRequest("http://domain.com/path")
.timeout(3, TimeUnit.SECONDS)
.send(new Response.CompleteListener()
{
@Override
public void onComplete(Result result)
{
// Your logic here
}
});
]]>
</script></div><p>The example above will impose a total timeout of 3 seconds on the request/response conversation.</p><p>The HTTP client APIs use listeners extensively to provide hooks for all possible request and response events,
and with JDK 8&#8242;s lambda expressions they&#8217;re even more fun to use:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
httpClient.newRequest("http://domain.com/path")
// Add request hooks
.onRequestQueued((request) -> { ... })
.onRequestBegin((request) -> { ... })
... // More request hooks available
// Add response hooks
.onResponseBegin((response) -> { ... })
.onResponseHeaders((response) -> { ... })
.onResponseContent((response, buffer) -> { ... })
... // More response hooks available
.send((result) -> { ... });
]]>
</script></div><p>This makes Jetty HTTP client suitable for HTTP load testing because, for example, you can accurately time
every step of the request/response conversation (thus knowing where the request/response time is really
spent).</p><p>Have a look at the <a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/client/api/Request.Listener.html" target="_top">
<code class="code">Request.Listener</code>
</a> class to know about request events, and to the <a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/client/api/Response.Listener.html" target="_top">
<code class="code">Response.Listener</code>
</a> class to know about response events.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="http-client-content"></a>Content Handling</h3></div></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="http-client-request-content"></a>Request Content Handling</h4></div></div></div><p>Jetty HTTP client provides a number of utility classes off the shelf to handle request content.</p><p>You can provide request content as <code class="code">String</code>, <code class="code">byte[]</code>, <code class="code">ByteBuffer</code>,
<code class="code">java.nio.file.Path</code>, <code class="code">InputStream</code>, and provide your own implementation of
<code class="classname">org.eclipse.jetty.client.api.ContentProvider</code>. Here&#8217;s an example that provides the
request content using <code class="code">java.nio.file.Paths</code>:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
ContentResponse response = httpClient.newRequest("http://domain.com/upload")
.file(Paths.get("file_to_upload.txt"), "text/plain")
.send();
]]>
</script></div><p>This is equivalent to using the <code class="code">PathContentProvider</code> utility class:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
ContentResponse response = httpClient.newRequest("http://domain.com/upload")
.content(new PathContentProvider(Paths.get("file_to_upload.txt")), "text/plain")
.send();
]]>
</script></div><p>Alternatively, you can use <code class="code">FileInputStream</code> via the <code class="code">
InputStreamContentProvider</code> utility class:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
ContentResponse response = httpClient.newRequest("http://domain.com/upload")
.content(new InputStreamContentProvider(new FileInputStream("file_to_upload.txt")), "text/plain")
.send();
]]>
</script></div><p>Since <code class="code">InputStream</code> is blocking, then also the send of the request will block if the
input stream blocks, even in case of usage of the asynchronous <code class="code">HttpClient</code> APIs.</p><p>If you have already read the content in memory, you can pass it as a <code class="code">byte[]</code> using the <code class="code">
BytesContentProvider</code> utility class:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
byte[] bytes = ...;
ContentResponse response = httpClient.newRequest("http://domain.com/upload")
.content(new BytesContentProvider(bytes), "text/plain")
.send();
]]>
</script></div><p>If the request content is not immediately available, but your application will be notified of the
content to send, you can use <code class="classname">DeferredContentProvider</code> in this way:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
DeferredContentProvider content = new DeferredContentProvider();
httpClient.newRequest("http://domain.com/upload")
.content(content)
.send(new Response.CompleteListener()
{
@Override
public void onComplete(Result result)
{
// Your logic here
}
});
// Content not available yet here
...
// An event happens, now content is available
byte[] bytes = ...;
content.offer(ByteBuffer.wrap(bytes));
...
// All content has arrived
content.close();
]]>
</script></div><p>While the request content is awaited and consequently uploaded by the client application, the server
may be able to respond (at least with the response headers) completely asynchronously.
In this case, <code class="code">Response.Listener</code> callbacks will be invoked before the request is fully sent.
This allows fine-grained control of the request/response conversation: for example the server may reject
contents that are too big, send a response to the client, which in turn may stop the content upload.</p><p>Another way to provide request content is by using an <code class="code">OutputStreamContentProvider</code>, which
allows applications to write request content when it is available to the <code class="code">OutputStream</code> provided by
<code class="code">OutputStreamContentProvider</code>:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
OutputStreamContentProvider content = new OutputStreamContentProvider();
// Use try-with-resources to close the OutputStream when all content is written
try (OutputStream output = content.getOutputStream())
{
client.newRequest("localhost", 8080)
.content(content)
.send(new Response.CompleteListener()
{
@Override
public void onComplete(Result result)
{
// Your logic here
}
});
...
// Write content
writeContent(output);
}
// End of try-with-resource, output.close() called automatically to signal end of content
]]>
</script></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="http-client-response-content"></a>Response Content Handling</h4></div></div></div><p>Jetty HTTP client allows applications to handle response content in different ways.</p><p>The first way is to buffer the response content in memory; this is done when using the blocking APIs (see
<a class="xref" href="http-client-api.html#http-client-blocking" title="Blocking APIs">Blocking APIs</a>) and the content is buffered within a <code class="code">ContentResponse</code> up to
2 MiB.</p><p>If you want to control the length of the response content (for example limiting to values smaller than the
default of 2 MiB), then you can use a
<code class="classname">org.eclipse.jetty.client.util.FutureResponseListener</code>in this way:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
Request request = httpClient.newRequest("http://domain.com/path");
// Limit response content buffer to 512 KiB
FutureResponseListener listener = new FutureResponseListener(request, 512 * 1024);
request.send(listener);
ContentResponse response = listener.get(5, TimeUnit.SECONDS);
]]>
</script></div><p>If the response content length is exceeded, the response will be aborted, and an exception will be thrown
by method <code class="code">get()</code>.</p><p>If you are using the asynchronous APIs (see <a class="xref" href="http-client-api.html#http-client-async" title="Asynchronous APIs">Asynchronous APIs</a>), you can use the <code class="code">
BufferingResponseListener</code> utility class:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
httpClient.newRequest("http://domain.com/path")
// Buffer response content up to 8 MiB
.send(new BufferingResponseListener(8 * 1024 * 1024)
{
@Override
public void onComplete(Result result)
{
if (!result.isFailed())
{
byte[] responseContent = getContent();
// Your logic here
}
}
});
]]>
</script></div><p>The second way is the most efficient (because it avoids content copies) and allows you to specify a <code class="code">
Response.ContentListener</code>, or a subclass, to handle the content as soon as it arrives:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
ContentResponse response = httpClient
.newRequest("http://domain.com/path")
.send(new Response.Listener.Empty()
{
@Override
public void onContent(Response response, ByteBuffer buffer)
{
// Your logic here
}
});
]]>
</script></div><p>The third way allows you to wait for the response and then stream the content using the <code class="code">
InputStreamResponseListener</code> utility class:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
InputStreamResponseListener listener = new InputStreamResponseListener();
httpClient.newRequest("http://domain.com/path")
.send(listener);
// Wait for the response headers to arrive
Response response = listener.get(5, TimeUnit.SECONDS);
// Look at the response
if (response.getStatus() == 200)
{
// Use try-with-resources to close input stream.
try (InputStream responseContent = listener.getInputStream())
{
// Your logic here
}
}
]]>
</script></div></div></div></div><script type="text/javascript">
SyntaxHighlighter.all()
</script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="http-client.html"><i class="icon-chevron-left"></i> Previous</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="http-client.html"><i class="icon-chevron-up"></i> Top</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="http-client-other.html">Next <i class="icon-chevron-right"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&nbsp;27.&nbsp;HTTP Client&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="icon-home"></i> Home</a></td><td width="40%" align="right" valign="top">&nbsp;Other Features</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout">
See an error or something missing?
<span class="callout"><a href="http://github.com/jetty-project/jetty-documentation">Contribute to this documentation at
<span class="website"><i class="icon-github"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2015-06-15T13:18:47-05:00)</i></span></div></p><script xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1149868-7']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script></body></html>