url - Proxy Check in Java -
i try check if proxy online or not. everytime exception thrown. tried system.setproperty (), doesn't work either...
my method:
public static boolean isonline(string host, int port) { string url = "http://www.google.com"; try { proxy proxy = new proxy(proxy.type.http, new inetsocketaddress(host, port)); httpurlconnection connection = (httpurlconnection) new url(url).openconnection(proxy); connection.connect(); return true; } catch (exception e) { return false; } }
the exception:
java.net.connectexception: connection refused: connect @ java.net.dualstackplainsocketimpl.connect0(native method) @ java.net.dualstackplainsocketimpl.socketconnect(dualstackplainsocketimpl.java:79) @ java.net.abstractplainsocketimpl.doconnect(abstractplainsocketimpl.java:345) @ java.net.abstractplainsocketimpl.connecttoaddress(abstractplainsocketimpl.java:206) @ java.net.abstractplainsocketimpl.connect(abstractplainsocketimpl.java:188) @ java.net.plainsocketimpl.connect(plainsocketimpl.java:172) @ java.net.socket.connect(socket.java:589) @ java.net.socket.connect(socket.java:538) @ sun.net.networkclient.doconnect(networkclient.java:180) @ sun.net.www.http.httpclient.openserver(httpclient.java:432) @ sun.net.www.http.httpclient$1.run(httpclient.java:484) @ sun.net.www.http.httpclient$1.run(httpclient.java:482) @ java.security.accesscontroller.doprivileged(native method) @ sun.net.www.http.httpclient.privilegedopenserver(httpclient.java:481) @ sun.net.www.http.httpclient.openserver(httpclient.java:522) @ sun.net.www.http.httpclient.<init>(httpclient.java:211) @ sun.net.www.http.httpclient.new(httpclient.java:308) @ sun.net.www.http.httpclient.new(httpclient.java:326) @ sun.net.www.protocol.http.httpurlconnection.getnewhttpclient(httpurlconnection.java:1167) @ sun.net.www.protocol.http.httpurlconnection.plainconnect0(httpurlconnection.java:1146) @ sun.net.www.protocol.http.httpurlconnection.plainconnect(httpurlconnection.java:997) @ sun.net.www.protocol.http.httpurlconnection.connect(httpurlconnection.java:931) @ viewer.proxycheck.isonline(proxycheck.java:18) @ viewer.viewer.main(viewer.java:7)
thanks time!
try this:
public static boolean isonline(){ system.setproperty("http.proxyhost", "failproxyip"); system.setproperty("http.proxyport", "failproxyport"); try { httpurlconnection connection = (httpurlconnection)new url("http://www.google.com").openconnection(); connection.connect(); return true; } catch (exception e) { return false; } } public static void main(string[] args) { system.out.println(isonline()); //prints false }
Comments
Post a Comment