Questions about java.net.URL and proxies
I've been reading http://java.sun.com/j2se/1.5.0/docs/guide/net/proxies.html and have had success with two methods (jdk 1.4 and 1.5)
System.getProperties().put("http.proxySet","true" );
System.getProperties().put("http.proxyHost","192.168.0.105");
System.getProperties().put("http.proxyPort","80" );
URL url =new URL("http://java.sun.com/j2se/1.5.0/docs/guide/net/proxies.html");
.. and
URL url =new URL("http","192.168.0.105", 80,"http://java.sun.com/j2se/1.5.0/docs/guide/net/proxies.html");
I have some basic questions:
1. Are there any adverse affects of using the system properties? What would be a concrete example?
2. Is the second option a valid way to connect through a proxy? Is either method preferred over the other?
3. Is there way to auto-detect proxy settings?
4. Some proxies require authentication. Is this the norm? In other words, should a typical connection settings screen provide an option for entering proxy user/password information?
Any insight or links would appreciated.
[1404 byte] By [
jDaze_a] at [2007-10-3 5:41:32]

> 1. Are there any adverse affects of using the system
> properties? What would be a concrete example?
No idea.
> 2. Is the second option a valid way to connect
> through a proxy?
Try it and see. If it works, I'd say it's valid.
> Is either method preferred over the
> other?
I don't know if there's a convention. I'd go with the second one if I were only connecting in one or two places in my code. If there were a lot of independent bits of code doing the connecting (which sounds like a bit of a dodgy design), I might go with the first option.
> 3. Is there way to auto-detect proxy settings?
No idea. But what do you mean?
> 4. Some proxies require authentication. Is this the
> norm? In other words, should a typical connection
> settings screen provide an option for entering proxy
> user/password information?
I don't know if it's the norm, but I'd certainly think the option to provide it should be there, since it's not all that uncommon.
jverda at 2007-7-14 23:49:22 >

>> Try it and see. If it works, I'd say it's valid.
I also prefer the first option, because it seems like you only have to set it once. But I wasn't sure from this comment what the pontential pitfalls might be...
"The major limitation of using system properties is that they are an 揳ll or nothing?switch. Meaning that once a proxy has been set for a particular protocol, it will affect all connections for that protocol. It's a VM wide behavior."
> 3. Is there way to auto-detect proxy settings?
>>No idea. But what do you mean?
Most browsers have an "auto-detect proxy settings for this network" option. So the browser transparently detects proxy settings instead of forcing the user enter the proxy host/port manually. Is there any way to do something similar with java or provide a "automatic proxy configuration from url" option?
Thanks for the input.
One other question, is authentication common in a corporate intranet enviornment?
> "The major limitation of using system properties
> is that they are an 揳ll or nothing?switch. Meaning
> that once a proxy has been set for a particular
> protocol, it will affect all connections for that
> protocol. It's a VM wide behavior."
If you're not doing other network communications in your program, this will not be a problem. If you are, it might be a problem, but then, it might not, as those other communications may also need to use the same proxy.
> > 3. Is there way to auto-detect proxy settings?
> >>No idea. But what do you mean?
>
> Most browsers have an "auto-detect proxy settings for
> this network" option. So the browser transparently
> detects proxy settings instead of forcing the user
> enter the proxy host/port manually.
Oh.
I assume they look at the registry or wherever stuff goes when you set your proxy in Internet Options. I doubt there's a pure Java way to do this.
> Is there any way
> to do something similar with java or provide a
> "automatic proxy configuration from url" option?
I don't know how that works, but if there's a standard format for proxy info to be retrieved, then yeah, you should be able to hit that URL and parse the results.
jverda at 2007-7-14 23:49:22 >

>>I doubt there's a pure Java way to do this.
I googled but didn't find much information, so I suspect you may be right.
>if there's a standard format for proxy info to be retrieved...
Good thought. There probably is a standard format. I'll look into it.
I'm not testing from an intranet right now, so I can't check but do you know if authentication common in a corporate intranet enviornment?
> I'm not testing from an intranet right now, so I
> can't check but do you know if authentication common
> in a corporate intranet enviornment?
I don't know how common it is in general. I know it was present in one of my last six or so jobs. It might have been present in one or two more, but I don't recall.
jverda at 2007-7-14 23:49:22 >

That gives at least it gives me an sense of it.I appreciate all your help.
Wait. You realize that the second form isn't valid according to the API docs or that document you linked to, right? The two forms are using system properties or using a Proxy class.
This article seems to describe proxy auto-config:
http://en.wikipedia.org/wiki/Proxy_auto-config
But from a quick skim I couldn't see how a browser would locate a default pac file for a network. Maybe it's in there somewhere.
> Wait. You realize that the second form isn't valid
> according to the API docs or that document you linked
> to, right? The two forms are using system properties
> or using a Proxy class.
I don't know if it's supposed to be possible to do it like that, but I have seen that form used in a number of programs, and it seemed to work fine when I used it too. It's certainly the simplest way of connecting through a proxy.
> I don't know if it's supposed to be possible to do it
> like that, but I have seen that form used in a number
> of programs, and it seemed to work fine when I used
> it too. It's certainly the simplest way of connecting
> through a proxy.
I'm glad you mentioned it. Yes, after doing more research I suspected it was not 'standard' since I found that example in a non-API source and haven't found any corresponding examples in the API yet. Since it appears to be un-documented, I probably will not use it. But I did like the fact that it seemed more flexible since it does not require changing system properties.
Yeah but the doc showed a way in JDK1.5 that doesn't uses system properties either, instead using a Proxy object, which personally I think is an even cleaner, more legible solution.
It's right there in the code, right next to the URL, and the purpose is clearly identified by the class name.
> Yeah but the doc showed a way in JDK1.5 that doesn't> uses system properties eitherI agree, but I need to support both 1.4 and 1.5 so I'm assuming I still have to use system properties for versions 1.4.
> System.getProperties().put( "http.proxySet", "true");BTW this does nothing in the JDK and never has.
ejpa at 2007-7-14 23:49:22 >

> > > System().put( "http.proxySet",
> "true");
>
>
> BTW this does nothing in the JDK and never has.
I tested it and you're right. Thanks for pointing it out. Who knows how long I would have been writing that particular line of code ....
Cheers