How to write REST client with proxy configurations?

Proxy?

Most of the enterprises have the proxy settings in-order to hide the actual endpoint from the client. Client will interact with the proxy, but the proxy will forward the request to the actual endpoint and return the response back to the client.

So what?

Till now we have seen code, how to communicate directly to the REST end-point. So the approach is straight forward. We don't need to have any special handling for the communication. Where-as if the REST service is hosted behind the proxy how to communicate?

Still thinking how? Don't worry it is very easy with Jersey client API.

client = ClientBuilder.newClient();
client.property(ClientProperties.PROXY_URI, "<proxy_host>:<proxy_port");
After created the client object, we have to add the property with specific with the proxy url configuration into the client.

In some cases, our network proxy has been restricted with user name and password. In such a situation we need to set the user name and password configuration into the client object as below,

client.property(ClientProperties.PROXY_USERNAME, "<username>");
client.property(ClientProperties.PROXY_PASSWORD, "<password>");
This is how we have to communicate with the REST end-point if our services are available behind proxy.

0 comments: