Search code examples
javauser-agent

Setting user agent of a java URLConnection


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?


Solution

  • 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=