Setup Proxy : OkHttpClient Proxy Settings

Setup Proxy – OkHttpClient Proxy Settings

This tutorial guides you on how to setup Proxy to connect to remote server. Therefore you would be able to connect remote server using proxyHost and proxyPort. In our example let’s see how to setup proxy using OkHttpClient with builder.

Setup Proxy : OkHttpClient Proxy Settings

Setup Proxy : OkHttpClient Proxy Settings

For instance, in my case the Socket IO Server is running in cloud (remote server) and I need to connect Socket IO Client running in Private Network which is behind corporate firewall to the remote server. Hence I need to setup proxy so that I will be able to connect to the remote server using proxyHost and proxyPort.

I tried the following solution and it worked for me.

First, I have added the following dependencies in my pom.xml.

1: Proxy Settings : pom.xml

I haved added OkHttp and Okio dependencies as shown below.

<!-- Proxy Settings -->

2: Proxy setup logic in the code

Basically, the piece of code below shows how to use OkHttpClient with builder to setup proxy in the socket io client code to connect to remote server.

Note, I have used PROXY_ENABLED flag to enable/ disable.

private static final string PROXY_ENABLED = "enable";

String remoteHost = ConfigurationProperties.getProperty("");
String remotePort = ConfigurationProperties.getProperty("remote-server.port");
String proxyHost = ConfigurationProperties.getProperty("");
int proxyPort = Integer.parseInt(ConfigurationProperties.getProperty("proxy.port"));

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
String proxyStatus = PropertyIdentifier.getProperty("proxy.status");

OkHttpClient client;
if (proxyStatus.equalsIgnoreCase(PROXY_ENABLED)) {
   client = new OkHttpClient.Builder().proxy(proxy).build();
}else {
   client = new OkHttpClient.Builder().proxy(Proxy.NO_PROXY).build();

IO.Options options = new IO.Options();
options.webSocketFactory = client;
options.callFactory = client;

socketio = IO.socket(URI.create("ws://"+remoteHost+":"+remotePort), options);
socketio = socketio.connect();


For example, the ConfigurationProperties file and files will look like below.


import java.util.Properties;

public class ConfigurationProperties{

private static Properties properties;
    static {
       properties = new Properties();
       URL url = new PropertyIdentifier().getClass().getClassLoader().getResource("");
           properties.load(new FileInputStream(url.getPath()));
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } catch (Exception e) {

     public static String getProperty(String key){
        return properties.getProperty(key);



That’s it. Whether it is socket io client or any other client that needs to connect to remote server using proxyHost and proxyPort you can follow this approach. Finally, you had learnt how to setup proxy programmatically to connect remote server via proxy server using OkHttpClient Proxy Settings.

Hope this article is helpful 🙂

You’ll also like:


Inline Feedbacks
View all comments