| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>Other Features</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 27. HTTP Client"><link rel="prev" href="http-client-api.html" title="API Usage"><link rel="next" href="websocket-intro.html" title="Chapter 28. WebSocket Introduction"><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">Other Features</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="http-client-api.html"><i class="icon-chevron-left"></i> Previous</a> </td><th width="60%" align="center">Chapter 27. HTTP Client<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right"> <a accesskey="n" href="websocket-intro.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-other"></a>Other Features</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="http-client-other.html#http-client-cookie">Cookies Support</a></span></dt><dt><span class="section"><a href="http-client-other.html#http-client-authentication">Authentication Support</a></span></dt><dt><span class="section"><a href="http-client-other.html#http-client-proxy">Proxy Support</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="http-client-cookie"></a>Cookies Support</h3></div></div></div><p>Jetty HTTP client supports cookies out of the box. The <code class="code">HttpClient</code> instance receives cookies from |
| HTTP responses and stores them in a |
| <code class="classname">java.net.CookieStore</code>, a class that is part of the JDK. When new requests are made, the |
| cookie store is consulted and if there are matching cookies (that is, cookies that are not expired and that match |
| domain and path of the request) then they are added to the requests.</p><p>Applications can programmatically access the cookie store to find the cookies that have been set:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[ |
| |
| CookieStore cookieStore = httpClient.getCookieStore(); |
| List<HttpCookie> cookies = cookieStore.get(URI.create("http://domain.com/path")); |
| |
| ]]> |
| </script></div><p>Applications can also programmatically set cookies as if they were returned from a HTTP response:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[ |
| |
| CookieStore cookieStore = httpClient.getCookieStore(); |
| HttpCookie cookie = new HttpCookie("foo", "bar"); |
| cookie.setDomain("domain.com"); |
| cookie.setPath("/"); |
| cookie.setMaxAge(TimeUnit.DAYS.toSeconds(1)); |
| cookieStore.add(URI.create("http://domain.com"), cookie); |
| |
| ]]> |
| </script></div><p>You can remove cookies that you do not want to be sent in future HTTP requests:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[ |
| |
| CookieStore cookieStore = httpClient.getCookieStore(); |
| URI uri = URI.create("http://domain.com"); |
| List<HttpCookie> cookies = cookieStore.get(uri); |
| for (HttpCookie cookie : cookies) |
| cookieStore.remove(uri, cookie); |
| |
| ]]> |
| </script></div><p>If you want to totally disable cookie handling, you can install a <code class="code">HttpCookieStore.Empty</code> instance |
| in this way:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[ |
| |
| httpClient.setCookieStore(new HttpCookieStore.Empty()); |
| |
| ]]> |
| </script></div><p>You can enable cookie filtering by installing a cookie store that performs the filtering logic in this |
| way:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[ |
| |
| httpClient.setCookieStore(new GoogleOnlyCookieStore()); |
| |
| public class GoogleOnlyCookieStore extends HttpCookieStore |
| { |
| @Override |
| public void add(URI uri, HttpCookie cookie) |
| { |
| if (uri.getHost().endsWith("google.com")) |
| super.add(uri, cookie); |
| } |
| } |
| |
| ]]> |
| </script></div><p>The example above will retain only cookies that come from the <code class="code">google.com</code> domain or |
| sub-domains.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="http-client-authentication"></a>Authentication Support</h3></div></div></div><p>Jetty HTTP client supports the "Basic" and "Digest" authentication mechanisms defined by <a class="link" href="http://www.ietf.org/rfc/rfc2617.txt" target="_top">RFC 2617</a>.</p><p>You can configure authentication credentials in the HTTP client instance as follows:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[ |
| |
| URI uri = new URI("http://domain.com/secure"); |
| String realm = "MyRealm"; |
| String user = "username"; |
| String pass = "password"; |
| |
| // Add authentication credentials |
| AuthenticationStore auth = httpClient.getAuthenticationStore(); |
| auth.addAuthentication(new BasicAuthentication(uri, realm, user, pass)); |
| |
| ContentResponse response = httpClient |
| .newRequest(uri) |
| .send() |
| .get(5, TimeUnit.SECONDS); |
| |
| ]]> |
| </script></div><p>Jetty HTTP client tests authentication credentials against the challenge(s) the server issues, and if they |
| match it automatically sends the right authentication headers to the server for authentication. If the |
| authentication is successful, it caches the result and reuses it for subsequent requests for the same domain and |
| matching URIs.</p><p>Successful authentications are cached, but it is possible to clear them in order to force authentication |
| again:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[ |
| |
| httpClient.getAuthenticationStore().clearAuthenticationResults(); |
| |
| ]]> |
| </script></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="http-client-proxy"></a>Proxy Support</h3></div></div></div><p>Jetty's HTTP client can be configured to use proxies to connect to destinations.</p><p>Two types of proxies are available out of the box: a HTTP proxy (provided by class |
| <code class="classname">org.eclipse.jetty.client.HttpProxy</code>) and a SOCKS 4 proxy (provided by class |
| <code class="classname">org.eclipse.jetty.client.Socks4Proxy</code>). |
| Other implementations may be written by subclassing <code class="classname">ProxyConfiguration.Proxy</code>.</p><p>A typical configuration is the following:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[ |
| |
| ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration(); |
| HttpProxy proxy = new HttpProxy("proxyHost", proxyPort); |
| // Do not proxy requests for localhost:8080 |
| proxy.getExcludedAddresses().add("localhost:8080"); |
| |
| httpClient.setProxyConfiguration(proxyConfig); |
| |
| ContentResponse response = httpClient.GET(uri); |
| |
| ]]> |
| </script></div><p>You specify the proxy host and port, and optionally also the addresses that you do not want to be proxied, |
| and then set the proxy configuration on the <code class="code">HttpClient</code> instance.</p><p>Configured in this way, <code class="code">HttpClient</code> makes requests to the HTTP proxy (for plain-text HTTP |
| requests) or establishes a tunnel via HTTP CONNECT (for encrypted HTTPS requests).</p></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-api.html"><i class="icon-chevron-left"></i> Previous</a> </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"> <a accesskey="n" href="websocket-intro.html">Next <i class="icon-chevron-right"></i></a></td></tr><tr><td width="40%" align="left" valign="top">API Usage </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"> Chapter 28. WebSocket Introduction</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> |