In this example we create a simple java application launched from command line. The application opens connection to three hosts: sourceforge.net, yahoo.com, apache.org. Than we instruct jProxyLoader to read configuration from two sources system properties config (defines one proxy) and custom configuration file proxies.properties which defines second proxy.
We start with creating the application:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class MyApp { public static void main(String[] args) throws IOException { System.out.println("Hello world"); System.out.println(Thread.currentThread().getContextClassLoader()); printResponseLength("http://sourceforge.net"); printResponseLength("http://yahoo.com"); printResponseLength("http://apache.org"); } private static void printResponseLength(String url) throws IOException { URL oracle = new URL(url); URLConnection yc = oracle.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); String inputLine; long responseLength = 0; while ((inputLine = in.readLine()) != null) { responseLength += inputLine.length(); } in.close(); System.out.println("The length of response for " + url + " is " + responseLength); } }
Now we create proxies.properties file on disk C: (assuming we are on windows machine) in directory tmp. We fill the contents of this file with definition of a proxy (in the example we use open http proxy located in US). As you can see we instruct jProxyLoader to pass connections to google.com via this proxy. The content of this file is following:
proxydef.usaproxy.hostname=216.74.224.14 proxydef.usaproxy.port=3128 proxydef.usaproxy.type=http proxydef.usaproxy.proxified.hosts=google.com
Second proxy is defined by means of system properties configuration. We will use open socks proxy located in South Korea. We define this proxy by passing following parameters to java program:
-DjplProxyDef.koreaproxy.hostname=101.55.12.75 -DjplProxyDef.koreaproxy.port=1080 -DjplProxyDef.koreaproxy.type=socks -DjplProxyDef.koreaproxy.proxifiedHosts=apache.org
We also pass "-DjplLoggingLevel=DEBUG" to see through which proxy the connection goes. Full command line to launch this app is following:
java -cp jproxyloader-1.0-RC1.jar -Djava.system.class.loader=net.sf.jproxyloader.JProxyLoader -DjplConfigFile=C:/tmp/proxies.properties -DjplProxyDef.koreaproxy.hostname=101.55.12.75 -DjplProxyDef.koreaproxy.port=1080 -DjplProxyDef.koreaproxy.type=socks -DjplProxyDef.koreaproxy.proxifiedHosts=apache.org -DjplLoggingLevel=DEBUG MyApp
After launching lot of debugging info will be printed to console. The most important lines are:
jProxyLoader: Matching proxy config: [http@216.74.224.14:3128#[sourceforge.net]] against hostname: sourceforge.net jProxyLoader: Found matching proxy: [http@216.74.224.14:3128#[sourceforge.net]] for host: sourceforge.net The length of response for http://sourceforge.net is 41222 jProxyLoader: Matching proxy config: [http@216.74.224.14:3128#[sourceforge.net]] against hostname: yahoo.com jProxyLoader: Matching proxy config: [socks@201.20.110.58:443#[apache.org]] against hostname: yahoo.com jProxyLoader: delegating to default proxy jProxyLoader: default proxies will be used : [DIRECT] The length of response for http://yahoo.com is 1387 jProxyLoader: Matching proxy config: [http@216.74.224.14:3128#[sourceforge.net]] against hostname: apache.org jProxyLoader: Matching proxy config: [socks@101.55.12.75:1080#[apache.org]] against hostname: apache.org jProxyLoader: Found matching proxy: [socks@101.55.12.75:1080#[apache.org]] for host: apache.org The length of response for http://apache.org is 40554
As you can see from the debugging messages connection to sourceforge.net goes through http proxy defined in properties file, connection to yahoo.com is direct connection and connection to apache.org goes via socks proxy defined within command line params.