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
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 --> <dependency> <groupId>com.squareup.okhttp</groupId> <artifactId>okhttp</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId>com.squareup.okio</groupId> <artifactId>okio</artifactId> <version>2.10.0</version> </dependency> ---
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("remote-server.host"); String remotePort = ConfigurationProperties.getProperty("remote-server.port"); String proxyHost = ConfigurationProperties.getProperty("proxy.host"); 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 application.properties files will look like below.
ConfigurationProperties
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; import java.util.Properties; public class ConfigurationProperties{ private static Properties properties; static { properties = new Properties(); URL url = new PropertyIdentifier().getClass().getClassLoader().getResource("application.properties"); try{ properties.load(new FileInputStream(url.getPath())); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static String getProperty(String key){ return properties.getProperty(key); } }
application.properties
remote-server.port=4040 remote-server.host=10.187.241.83 proxy.host=23.45.29.251 proxy.port=8086 proxy.status=enable
Conclusion
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:
- Spring Boot App Program Error: Could not find or load main class
- Build maven project without version
- Assign value to static variables from application.properties in Spring Boot ?
- Create JWT Token and Sign with RSA Private Key
- Replace element in ArrayList at specific index
- Convert Integer List to int array
- ArrayList removeAll() method not removing elements
- Convert floating point number to fixed point in Python
- What is %matplotlib inline and how to use ?
- Java String substring() example program
- How to change the default theme in Jupyter Notebook ?
- Websocket connection closed automatically – keepalive Ping example
- Check if a file or folder exists without getting exceptions ?
- Python program to find the greatest of three numbers
- Putty Fatal Error No supported authentication methods available
- Find which users belongs to a specific group in linux
- Check if Python Object is a Number ?
- Remove non-numeric characters from string in Python
- Convert negative to positive number in Python
- Extract numbers from a string in python
- Program to Check Given Number is Odd or Even
- Jacoco Maven Plugin Junit Code Coverage Example