How to check connection through a proxy server using java?

I have code to check the proxy server and port, some of these characters:

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 the openConnection () method will do the following:

  • try connecting this url using a proxy server.
  • If he cannot use the proxy server, he will directly bind the URL without a proxy server .

that is the problem, I wanted to check if the proxy server is working, but this code will not stop if the proxy server cannot connect.

I also tried using the isReachable () method of the InetAddress class, but I get the same result.

so how can I stop this connection if the proxy server is down to check if the proxy is available?

+4
source share
2 answers

Sorry guys, I found out how to do this. I used the java.net.Proxy class to open a connection through a proxy. It is easy to use and works great. See Java Networking and Proxies

+1
source

System.getProperties (). Put ("ProxySet", "Truth");

It does nothing. This is an urban myth.

+1
source