6: Making requests via proxies

HTTP allows connections to pass from the client to the server via optional intermediaries called proxies. Proxies are servers on the network that receive your requests and pass them on to the origin server on your behalf (possibly via yet more proxies). There are several types of proxies but we’re only concerned with ‘ordinary’ proxies here, i.e. the kind of proxies you have to explicitly set up your client to use (transparent proxies and reverse proxies are not relevant here). The HTTP standard specifies how proxies should work. Note that it is possible that you may send or receive additional headers when you make connections via proxies.

Bee Client supports proxies using the underlying Java API. There are two approaches available and you choose what you want via the proxy field of Config

  • Pass a Proxy in via the Config - this is compiled into your app but does not require any command-line flags. Also, it allows various connections to use different proxies at the same time by using different Configs.
  • Set the command-line flags - this affects all HTTP requests but does not require any code change other than to pass None as the proxy setting in your Config (this is not the default).

If you don’t specify proxy settings, you get this default:

  val config1 = Config()
  val config2 = Config(proxy = Some(Proxy.NO_PROXY))

These two have the same effect, which means that neither of the two proxy techniques are in operation.

We’ll discuss both proxying techniques in turn now.

Using a Proxy object

When you construct an HttpClient, it accepts a Config configuration object that has a proxy parameter which is standard Java API.

For example, suppose my proxy is localhost:8888

import java.net._

  ...
  val proxyAddress = new InetSocketAddress("localhost", 8888)
  val proxy = new Proxy(Proxy.Type.HTTP, proxyAddress)
  val config = Config(proxy = Some(proxy))
  val httpClient = new HttpClient(config)

Subsequent requests using this httpClient will be routed via localhost:8888.

Using the JVM command-line properties

If you prefer to use the JVM command-line properties, you need to construct your Config accordingly.

  val config = Config(proxy = None) // revert to the JVM DefaultProxySelector
  val httpClient = new HttpClient(config)

The command-line properties you can set include:

  • http.proxyHost
  • http.proxyPort
  • http.nonProxyHosts
  • http.proxyUser
  • http.proxyPassword
  • htttps.proxyHost
  • https.proxyPort

These are passed to the JVM via -D at startup (they are not environment variables in your OS). The JVM documentation provides more detail.

There is never any need to set any of these properties via System.setProperty(String, String), because the new Proxy method above is easier.