How to Configure HTTP/HTTPS Proxy Settings Java JVM


Many times java programs need to connect the internet. If your application server is not allowed to connect the internet then you should use proxy to open a  gateway to connect internet over proxy. At this post, I wrote a basic java code and also show how to set proxy configuration at JVM side from the command  line.

Step 1: Compile Java  Code for Connection Test

I added commands for the Linux server. But it's not different for other  OS too. Same operation can be performed any kind of operating system which Java compiler installed.

  • First Create a file which  named "URLConnectionReader.java" under  /tmp directory
#vi  /tmp/URLConnectionReader.java
  • Second add  codes  to file and  save  it.
import java.net.*;
import java.io.*;

public class URLConnectionReader {
public static void main(String[] args) throws Exception {

System.setProperty("javax.net.debug", "all");
System.setProperty("https.protocols", "TLSv1.1,TLSv1.2");
java.lang.System.setProperty(
"jdk.tls.client.protocols", "TLSv1,TLSv1.1,TLSv1.2");
URL bkmexpress = new URL(args[0]);
URLConnection yc = bkmexpress.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
  • Then last step  compile  java codes.
#/usr/java/jdk1.8.0_111/bin/javac  /tmp/URLConnectionReader.java

Step 2: Test Connection With Proxy

-You need to run this command  under where your "URLConnectionReader.class" file created. "URLConnectionReader.class"  is  the  compiled file when you run javac  command at step 1.

#/usr/java/jdk1.8.0_111/bin/java -Dhttp.proxyHost=casesupproxysrv01 -Dhttp.proxyPort=8080  URLConnectionReader http://casesup.com

 

I'm a IT Infrastructure and Operations Architect with extensive experience and administration skills and works for Turk Telekom. I provide hardware and software support for the IT Infrastructure and Operations tasks.

205 Total Posts
Follow Me