Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Using RestEasyClient with Proxies

Header Image

Ahoy there mateys! If ye be lookin’ to use RestEasyClient with a proxy, ye’ve come to the right place. In this article, we’ll be discussing how to configure RestEasyClient to work with proxies. But first, let’s take a look at what a proxy is and why ye might need one.

A proxy is like a middleman between yer computer and the internet. It intercepts all requests ye make and forwards them on yer behalf. Why might ye need a proxy, ye ask? Well, there are a few reasons.

One reason is if yer network administrator has blocked certain websites or restricted access to the internet. By using a proxy, ye may be able to bypass these restrictions and access the sites ye need. Another reason is to increase yer privacy and security. A proxy can hide yer IP address and encrypt yer data, making it more difficult for others to track or intercept yer online activity.

Now, let’s get to the meat of the matter: configuring RestEasyClient to work with a proxy. To do this, we need to set a few configuration options.

Configuring RestEasyClient for Proxy Usage

The first thing ye need to do is create an instance of the RestEasyClient class. This can be done like so:

ResteasyClient client = new ResteasyClientBuilder().build();

Once ye have yer client, ye can set the proxy configuration like this:

client.property(ClientProperties.PROXY_URI, "http://my.proxy.server:8080");

This sets the URI of the proxy server ye want to use. But wait, there’s more! Ye can also set the username and password if yer proxy requires authentication:

client.property(ClientProperties.PROXY_USERNAME, "username");
client.property(ClientProperties.PROXY_PASSWORD, "password");

And that’s it! Ye’re now ready to use RestEasyClient with a proxy. But before we let ye go, let’s take a quick look at some of the other proxy-related options ye can set.

Setting Proxy Options

There are a few other options ye can set when using RestEasyClient with a proxy. These are set using the ClientProperties class, just like the proxy URI, username, and password.

Proxy Type

The type of proxy ye want to use can be set using the PROXY_TYPE property. The default is HTTP, but ye can also set it to SOCKS.

client.property(ClientProperties.PROXY_TYPE, ProxyType.SOCKS);

Proxy Host and Port

If ye want to specify the host and port of yer proxy server separately, ye can use the PROXY_HOST and PROXY_PORT properties.

client.property(ClientProperties.PROXY_HOST, "my.proxy.server");
client.property(ClientProperties.PROXY_PORT, 8080);

Non-Proxy Hosts

Ye may want to exclude certain hosts from being routed through yer proxy. To do this, ye can set the NON_PROXY_HOSTS property.

client.property(ClientProperties.NON_PROXY_HOSTS, "localhost|*.mycompany.com");

This sets a list of hosts that will not be routed through the proxy. Ye can use the pipe character (|) to separate multiple hosts.

And there ye have it, me hearties! Ye now know how to configure RestEasyClient to work with a proxy, as well as some of the other proxy-related options ye can set. So go forth and make yer requests securely and with privacy! Arrr!

Setting Proxy Options (continued)

Proxy Exceptions

In some cases, ye may want to exclude certain URLs or domains from being routed through the proxy. To do this, ye can set the http.nonProxyHosts system property.

System.setProperty("http.nonProxyHosts", "localhost|*.mycompany.com");

This sets a list of hosts that will not be routed through the proxy. Ye can use the pipe character (|) to separate multiple hosts.

Proxy Precedence

If ye set multiple proxy-related properties, RestEasyClient will use them in the following order of precedence:

  1. PROXY_URI
  2. PROXY_HOST and PROXY_PORT
  3. http.proxyHost and http.proxyPort system properties
  4. https.proxyHost and https.proxyPort system properties

So, if ye set the PROXY_URI property, it will take precedence over all the others.

Conclusion

And there ye have it, me hearties! Ye now know how to configure RestEasyClient to work with a proxy and some of the other proxy-related options ye can set. By using a proxy, ye can bypass network restrictions, increase yer privacy and security, and generally have a better time on the high seas of the internet. So hoist the Jolly Roger and set sail with RestEasyClient and a trusty proxy by yer side!