views:

1274

answers:

2

I'm trying to parse a webpage using Java with URLConnection. I try to set up the user-agent like this:

java.net.URLConnection c = url.openConnection();
c.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

But the resulting user agent is the one I specify, with "Java/1.5.0_19" appended to the end. Is there a way to truly set the user agent without this addition?

+2  A: 

Off hand, setting the http.agent system property to "" might do the trick (I don't have the code in front of me).

You might get away with:

 System.setProperty("http.agent", "");

but that might require a race between you and initialisation of the URL protocol handler, if it caches the value at startup (actually, I don't think it does).

The property can also be set through JNLP files (available to applets from 6u10) and on the command line:

-Dhttp.agent=

Or for wrapper commands:

-J-Dhttp.agent=
Tom Hawtin - tackline
How would I do that? c.setRequestProperty("http.agent","");? I'm assuming somewhere else...
DiglettPotato
http://www.innovation.ch/java/HTTPClient/advanced_info.html -> http.agent
Karussell
@diglettpotato see also http://www.ivoa.net/forum/apps/0903/0610.htm
Karussell
@diglettpotato I'm missing the word system. System property. Answer edits...
Tom Hawtin - tackline
A: 

Slightly changed Tom Hawtins answer to:

 System.setProperty("http.agent", ""); 

according to http://www.ivoa.net/forum/apps/0903/0610.htm

Karussell