Using ProxySelector to allow users to configure the proxy

Jean-Christophe Collet Jean-Christophe.Collet at Sun.COM
Mon May 4 06:09:56 PDT 2009


Paulo Levi wrote:
> But it's completely unintuitive! I can't know if i need to bother the 
> user about proxy configuration before trying with no proxy, but trying 
> to use that disallows the use of any other.
>
> Trying to wrap Proxy.NO_PROXY to circumvent that, even if technically 
> possible, just makes the code fail in the next line.
>
> if (p.type() != Proxy.Type.SOCKS)
>             throw new SocketException("Unknown proxy type : " + p.type());
>
> I'm doing this because it's the natural way to create the behavior i 
> want, only bugging the user for the proxy if needed.
> If you have another idea that doesn't involve bogus requests to 
> google.com <http://google.com> or other "know assumed forever" sites 
> i'd like to hear it.

The most likely way to tell that you're behind a firewall is to catch 
NoRouteToHostException which is thrown when trying to connect to an 
address that can't be reached. As it is hinted in its javadoc: 
http://java.sun.com/javase/6/docs/api/java/net/NoRouteToHostException.html
However, firewalls vary in nature, so there is no unique way to detect 
them. For instance in some cases you won't even be able to resolve the 
URL hostname because the internal DNS won't resolve external addresses.
There is a reason web browsers don't auto-detect you're behind a 
firewall....

Your best bet, I think, is to attempt a direct connection while catching 
both NoRouteToHostException and UnknownHostException. If either of these 
happen, assuming you're certain of your host name, chances are you are 
behind a firewall.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: jean-christophe_collet.vcf
Type: text/x-vcard
Size: 246 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/net-dev/attachments/20090504/c3e3eff0/jean-christophe_collet.vcf 


More information about the net-dev mailing list