views:

31

answers:

2

i have some code to test if the proxy server and port is working ,some of the code like this:

System.getProperties().put("proxySet", "true");
System.getProperties().put("https.proxyHost", "localhost");
System.getProperties().put("https.proxyPort", "1234");
System.getProperties().put("http.proxyHost", "localhost");
System.getProperties().put("http.proxyPort", "1234");
HttpURLConnection conn = (HttpURLConnection) new URL("https://www.google.com").openConnection();
conn.getContent();
conn.disconnect();

it seems that openConnection() method will do thing like this:

  1. try to connect given URL using proxy.
  2. if it fails to use proxy,it will connect URL directly without proxy.

that's the problem,i meant to test if the proxy is working,but this code won't stop if the proxy can not connect.

i also tried to use isReachable() method of InetAddress class,but i get the same result.

so how could i stop this connection if the proxy doesn't work ,in order to test if the proxy is reachable ?

+1  A: 

sorry guys. i have find out the way. i used java.net.Proxy class to open a connection via proxy. it's easy to use and works fine. the page below shows how.
http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

Barry Wei
A: 

System.getProperties().put("proxySet", "true");

That one doesn't do anything. It is an urban myth.

EJP