We help IT Professionals succeed at work.
Get Started

Setting user agent in Java appends java version at the end when using the URL class

Last Modified: 2012-05-11

If I use:

System.setProperty("http.agent","Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv: Gecko/20100316 Firefox/3.6.2");

I can set up the user agent just fine within Java 1.6

But, then if I use the URL class it will append Java 1.6.0_22 to it so that the string on the server will be:

"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv: Gecko/20100316 Firefox/3.6.2 Java/1.6.0_22"

The URL class has, as one of its methods, url.openConnection().setDefaultRequestProperty(key, value), which is deprecated and does not do the trick anymore,

I was able to do that perfectly with httpunit, however I need to actually accomplish that with the Java URL class.

With httpunit, this is how I've done it and the result is perfect without the java 1.6 appended at the end:

WebConversation wc = new WebConversation();
wc.getClientProperties().setUserAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv: Gecko/20100316 Firefox/3.6.2");

So, how can I really have control of setting the user agent in Java so that the URL class will not append any extra information to it?

Watch Question
Distinguished Expert 2020
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE