| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>Proxy Support</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.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"><link rel="up" href="http-client.html" title="Chapter 22. HTTP Client"><link rel="prev" href="http-client-authentication.html" title="Authentication Support"><link rel="next" href="http-client-transport.html" title="Pluggable Transports"><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"><link rel="stylesheet" href="css/highlighter/foundation.css"><script src="js/highlight.pack.js"></script><script> |
| hljs.initHighlightingOnLoad(); |
| </script><link type="text/css" rel="stylesheet" href="css/font-awesome/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.4.28-SNAPSHOT</span></td><td style="width: 50%"></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">Proxy Support</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="http-client-authentication.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a> </td><th width="60%" align="center">Chapter 22. HTTP Client<br><a accesskey="p" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></th><td width="20%" align="right"> <a accesskey="n" href="http-client-transport.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></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 for sponsored feature development |
| </p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="http-client-proxy"></a>Proxy Support</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="section"><a href="http-client-proxy.html#http-client-proxy-authentication">Proxy Authentication Support</a></span></dt></dl></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="literal">org.eclipse.jetty.client.HttpProxy</code>) and a SOCKS 4 proxy (provided by class <code class="literal">org.eclipse.jetty.client.Socks4Proxy</code>). |
| Other implementations may be written by subclassing <code class="literal">ProxyConfiguration.Proxy</code>.</p><p>The following is a typical configuration:</p><pre 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"><code>ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration(); |
| HttpProxy proxy = new HttpProxy("proxyHost", proxyPort); |
| |
| // Do not proxy requests for localhost:8080 |
| proxy.getExcludedAddresses().add("localhost:8080"); |
| |
| // add the new proxy to the list of proxies already registered |
| proxyConfig.getProxies().add(proxy); |
| |
| ContentResponse response = httpClient.GET(uri);</code></pre><p>You specify the proxy host and port, and optionally also the addresses that you do not want to be proxied, and then add the proxy configuration on the <code class="literal">ProxyConfiguration</code> instance.</p><p>Configured in this way, <code class="literal">HttpClient</code> makes requests to the HTTP proxy (for plain-text HTTP requests) or establishes a tunnel via <code class="literal">HTTP CONNECT</code> (for encrypted HTTPS requests).</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="http-client-proxy-authentication"></a>Proxy Authentication Support</h3></div></div></div><p>Jetty’s HTTP client support proxy authentication in the same way it supports <a class="link" href="http-client-authentication.html" title="Authentication Support">server authentication</a>.</p><p>In the example below, the proxy requires Basic authentication, but the server requires Digest authentication, and therefore:</p><pre 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"><code>URI proxyURI = new URI("http://proxy.net:8080"); |
| URI serverURI = new URI("http://domain.com/secure"); |
| |
| AuthenticationStore auth = httpClient.getAuthenticationStore(); |
| |
| // Proxy credentials. |
| auth.addAuthentication(new BasicAuthentication(proxyURI, "ProxyRealm", "proxyUser", "proxyPass")); |
| |
| // Server credentials. |
| auth.addAuthentication(new DigestAuthentication(serverURI, "ServerRealm", "serverUser", "serverPass")); |
| |
| // Proxy configuration. |
| ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration(); |
| HttpProxy proxy = new HttpProxy("proxy.net", 8080); |
| proxyConfig.getProxies().add(proxy); |
| |
| ContentResponse response = httpClient.newRequest(serverURI) |
| .send() |
| .get(5, TimeUnit.SECONDS);</code></pre><p>The HTTP conversation for successful authentications on both the proxy and the server is the following:</p><pre class="screen">Application HttpClient Proxy Server |
| | | | | |
| |--- GET -->|------------- GET ------------->| | |
| | | | | |
| | |<----- 407 + Proxy-Authn -------| | |
| | | | | |
| | |------ GET + Proxy-Authz ------>| | |
| | | | | |
| | | |---------- GET --------->| |
| | | | | |
| | | |<--- 401 + WWW-Authn ----| |
| | | | | |
| | |<------ 401 + WWW-Authn --------| | |
| | | | | |
| | |-- GET + Proxy-Authz + Authz -->| | |
| | | | | |
| | | |------ GET + Authz ----->| |
| | | | | |
| |<-- 200 ---|<------------ 200 --------------|<--------- 200 ----------|</pre><p>The application does not receive events related to the responses with code 407 and 401 since they are handled internally by <code class="literal">HttpClient</code>.</p><p>Similarly to the <a class="link" href="http-client-authentication.html" title="Authentication Support">authentication section</a>, the proxy authentication result and the server authentication result can be preempted to avoid, respectively, the 407 and 401 roundtrips.</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-authentication.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a> </td><td width="20%" align="center"><a accesskey="u" href="http-client.html"><i class="fa fa-chevron-up" aria-hidden="true"></i> Top</a></td><td width="40%" align="right"> <a accesskey="n" href="http-client-transport.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Authentication Support </td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></td><td width="40%" align="right" valign="top"> Pluggable Transports</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/eclipse/jetty.project">Contribute to this documentation at |
| <span class="website"><i class="fa fa-github" aria-hidden="true"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2020-03-10)</i></span></div></p></body></html> |