If you are inside a firewall and want your Java app to connect to the Internet then you have to connect to the proxy.
Any corporate environment you mostly live and work inside the Firewall. So mostly you will have the challenge to connect to the internet when you write Java standalone, web or console application. In this article, we try to see how we can easily connect to a local firewall or proxy by setting our code using Java programming
When you work under a proxy and like to run your app in you have to set up the proxy setting for your Java app.
Java provides following way to work in a Proxy environment:
Proxy setup in Java provides both HTTP and HTTPS.
http.proxyHost – host name/ip of the proxy
http.proxyPort – port of the proxy
For HTTPs: instead of HTTP give HTTPS as a prefix
System Property: System class having property settings, through this you can enable proxy setting through programmatically
In your java code, you may give inside a static block
System.setProperty("http.proxyHost", "<HOST>"); System.setProperty("http.proxyPort", "<PORT>");
To clear the setting use
System.clearProperty("http.proxyHost");
Command Line: If you don’t like to hardcode in code you can use switch for your java command also as follows
$ java -Dhttp.proxyHost=<HOST> -Dhttp.proxyPort=<PORT> HelloWebWorld
If you want to read other Java-related articles check my Java Category
If you like to follow me, do not forget to leave your email here. You get the new post and useful information directly to your inbox, as and when its posted.
We do not sell your email address!