0

I am looking for a way to programmatically change Windows Internet Options (proxy settings more specific). I saw in C# there is a class called InternetSetOption which I believe will do want I need. I was wondering is there a Java equivalent?

If not is there anyway I can in Java change Windows network proxy settings INSTANTLY because changing registry would work but you need to reboot or restart explorer.exe which aren't options for this application.

1 Answers1

0

I think I found what you are looking for here.

Here is some example code:

//Set the http proxy to webcache.mydomain.com:8080

System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setPropery("http.proxyPort", "8080");

// Next connection will be through proxy.
URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();

// Now, let's 'unset' the proxy.
System.setProperty("http.proxyHost", null);

// From now on http connections will be done directly.
Now,this works reasonably well, even if a bit cumbersome, but it can get tricky if your application is multi-threaded. Remember, system properties are “VM wide” settings, so all threads are affected. Which means that the code in one thread could, as a side effect, render the code in an other thread inoperative.

EDIT:

Here is some more specific examples:

Let's look at a few examples assuming we're trying to execute the main method of the GetURL class:

$ java -Dhttp.proxyHost=webcache.mydomain.com GetURL
All http connections will go through the proxy server at webcache.mydomain.com listening on port 80 (we didn't specify any port, so the default one is used).

$ java -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080
-Dhttp.noProxyHosts=”localhost|host.mydomain.com” GetURL
In that second example, the proxy server will still be at webcache.mydomain.com, but this time listening on port 8080. Also, the proxy won't be used when connecting to either localhost or host.mydonain.com.

EDIT 2:

Perhaps something along these lines then:

System.setProperty( "http.proxyHost", "webcache.mydomain.com" );
System.setProperty( "http.proxyPort", "8080" );

System.setProperty( "https.proxyHost", "webcache.mydomain.com" );
System.setProperty( "https.proxyPort", "8080" );
Melis
  • 111
  • 9
  • I believe this would only set the proxy for the Java Virtual machine, what I need is to change the Windows system proxy (located in Internet Options control panel). – CyrodiilSavior Feb 10 '16 at 23:47
  • Please have a look at EDIT 2, that might be more what you were looking for – Melis Feb 10 '16 at 23:57
  • I now realize that wasnt much of a change, how come you need to set the system property btw? – Melis Feb 11 '16 at 00:01
  • I'm writing a program that establishes an SSH connection to a remote server then sets that connection up as a Proxy Server on 127.0.0.1:8080 Then in the OS change system proxy settings to look for a SOCKS proxy on 127.0.0.1:8080 This works fine on Mac and Linux because you can issue commands to make it happen, windows has been a pain. – CyrodiilSavior Feb 11 '16 at 00:04
  • Thats actually what I'm researching this very moment. I know we tried before but had some issues (not sure 100% what) but I'm going to try to fight with it a bit more. – CyrodiilSavior Feb 11 '16 at 00:13
  • Are you farmiliar with netsh? because depending on what happens I may have questions. – CyrodiilSavior Feb 11 '16 at 00:17
  • I have some familiarity with it, would help if you could tell me what is going wrong – Melis Feb 11 '16 at 00:34
  • Just finished setting up a VM to test it on. I'll let you know what happens. – CyrodiilSavior Feb 11 '16 at 00:34
  • command should be something along the lines of: netsh winhttp set proxy yourproxy:port – Melis Feb 11 '16 at 00:37
  • I tried netsh winhttp set proxy proxy-server="socks=localhost:8080" and it seems to have set it up but when I go to ipmonkey.com my ipaddress is still the same (not using the one being broadcast over 8080) – CyrodiilSavior Feb 11 '16 at 01:02
  • After some reasearch it seems that this method only helps with applications that uses the winhttp library, which means I am out of suggestions – Melis Feb 11 '16 at 11:55
  • I came to the same conclusion last night. Thanks for your help. – CyrodiilSavior Feb 11 '16 at 18:57