Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Apache HTTP Client 3.1 will not natively honor HTTP Proxies returned from the default ProxySelector or user implementations.</p> <h2>Quick Summary of ProxySelector</h2> <p>ProxySelector is a service class which selects and returns a suitable Proxy for a given URL based on its scheme. For example, a request for <code>http://somehost</code> will try to provide an HTTP proxy if one is defined. The default ProxySelector can be configured at runtime using System Properties, such as <code>http.proxyHost</code> and <code>http.proxyPort</code>. </p> <h2>HTTPUrlConnection</h2> <p>An instance of HTTPUrlConnection will check against the default ProxySelector multiple times: 1st to select for <code>http</code> or <code>https</code>, then later when it builds the raw tcp socket, using the <code>socket</code> scheme. A SOCKS proxy could be used to proxy a raw tcp socket but are not often found in corporate environments, so a raw tcp socket will usually receive no proxy.</p> <h2>HTTP Client 3.1</h2> <p>HC 3.1, on the other hand, will <strong>never check the default ProxySelector for the <code>http</code>/<code>https</code> schemes</strong>. It will check, however, at a later points for the <code>socket</code> scheme when it eventually builds the raw socket - This is the request you are seeing. This means the System Properties <code>http.proxyHost</code> and <code>http.proxyPort</code> are ineffective. This is obviously not ideal for most people who only have an HTTP/HTTPS proxy.</p> <p>To work around this, you have two options: define a proxy on each HC 3.1 connection or implement your own HC 3.1 HTTPConnectionManager.</p> <h3>HTTPConnectionManager</h3> <p>The HTTPConnectionManager is responsible for building connections for the HC 3.1 client.</p> <p>The default HC 3.1 HTTPConnectionManager can be extended so that it looks for a suitable proxy from a ProxySelector (default or custom) when building the request in the same way HTTPUrlConnection does:</p> <pre><code>public class MyHTTPConnectionManager extends SimpleHttpConnectionManager { @Override public HttpConnection getConnectionWithTimeout( HostConfiguration hostConfiguration, long timeout) { HttpConnection hc = super.getConnectionWithTimeout(hostConfiguration, timeout); try { URI uri = new URI( hostConfiguration.getHostURL()); List&lt;Proxy&gt; hostProxies = ProxySelector.getDefault().select(uri); Proxy Proxy = hostProxies.get(0); InetSocketAddress sa = (InetSocketAddress) Proxy.address(); hc.setProxyHost(sa.getHostName()); hc.setProxyPort(sa.getPort()); } catch (URISyntaxException e) { return hc; } return hc; } } </code></pre> <p>Then, when you create an HC 3.1 client, use your new connection manager:</p> <pre><code>HttpClient client = new HttpClient(new MyHTTPConnectionManager() ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload