You are here

Tutorial: 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 JVM API. There are two approaches available:

  • Set the command-line flags - this affects all HTTP requests but does not require any code change
  • 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.

Using the JVM command-line properties

The 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 no need to set any of these properties via System.setProperty(String, String), because the following method is easier.

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 = proxy)
val httpClient = new HttpClient(config)

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