Posts Tagged ‘proxy’

how to get a ProxyEnable value from windows registry in java

Thursday, October 9th, 2008
I have developed a webservice which checks if a new version of software exists. Webservice works fine if the user does not use a proxy otherwise it raises an error “connection timeout….”.
 
If the user uses a proxy then the program should read a proxy settings first. I have found a lot of examples how to read a proxy settings from windows registry and this is not a problem at all (search for JavaRegistryHack).
 
The problem occurs when a user has VPN. In my example I use a proxy when I am connected to VPN but I do not have a proxy when I do not use VPN. I always get proxy address and proxy port regardless of the value in check box “Use a proxy server for your LAN”.
 
 
How to find out if the value of upper checkbox is selected? The answer is in a registry value ProxyEnable. ProxyEnable is set to 0 if a proxy is not selected and it has a value 1 if the proxy is used.
 
But there is another problem. There are two different types of values in registry: REG_SZ and REG_DWORD. It is very easy to read a value for REG_SZ but I have wasted a lot of time to find out how to read a value for REG_DWORD type. Below is a function which is used to read a ProxyEnable. You can use this function also for other REG_DWORD values.
import java.io.*;
public class ProxyEnable {
  private static final String REGQUERY_UTIL = “reg query “;
  private static final String REGDWORD_TOKEN = “REG_DWORD”;
  private static final String PROXYENABLE = REGQUERY_UTIL +
   “\”HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\”" 
   + ” /v ProxyEnable”;
  public static String getProxyEnable() {
    try {
      Process process = Runtime.getRuntime().exec(PROXYENABLE);
      StreamReader reader = new StreamReader(process.getInputStream());
      reader.start();
      process.waitFor();
      reader.join();
      String result = reader.getResult();
      int p = result.indexOf(REGDWORD_TOKEN);
      if (p == -1)
         return null;
      // value is 0×0 or 0×1
      String temp = result.substring(p + REGDWORD_TOKEN.length()).trim();
      temp = temp.substring(2, temp.length());
      return Integer.toString
          ((Integer.parseInt(temp)));
    }
    catch (Exception e) {
      return null;
    }
  }
  static class StreamReader extends Thread {
    private InputStream is;
    private StringWriter sw;
    StreamReader(InputStream is) {
      this.is = is;
      sw = new StringWriter();
    }
    public void run() {
      try {
        int c;
        while ((c = is.read()) != -1)
          sw.write(c);
        }
        catch (IOException e) { ; }
      }
    String getResult() {
      return sw.toString();
    }
  }
  public static void main(String s[]) {
    System.out.println(“Proxy Enable : ” + getProxyEnable());
  }
}
I hope this will help someone oneday somewhere…

Do you find this post useful? Do not hesitate to leave a reply! Go to the bottom of this page.