简体   繁体   中英

how to test proxy internet connection using java?

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 ?

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

That one doesn't do anything. It is an urban myth. It was part of the defunct 1997 HotJava bean and leaked from there into various books. It has never been part of any JDK. Try setting it to false in some situation where you need it on and see for yourself.

Sorry guys, I found out the way to do it. I used java.net.Proxy class to open a connection via proxy. It's easy to use and works fine. See Java Networking and Proxies

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM