How to emulate MSIE or Netscape in URLConnection?

Posted on 2001-06-27
Last Modified: 2010-05-18

I have a spider program project and there are websites
wherein I get a different webpage when using MS Internet
Explorer compared to using my Java code which
looks like the ff:
   URLConnection uc = new URLConnection("");
   InputStream is = uc.getInputStream();
   int b =;
   String pagestr = new String();
   while (b != -1){
      pagestr += /* concatenate all the read bytes */
      b =;

The Java code above usually gets an error message
webpage.  It's like the HTTP server detects what type of
browser is accessing their pages.  If it's not Mozilla
compatible then it gives an error message webpage.

How do I write my Java code so as the webserver thinks
that my Java code is a Mozilla compatible browser?

I've seen a couple of forum discussions in
about the GET header their getting but I don't know how
to make Java code to do what their discussing.

Thanks in advance!
Question by:joerev

Accepted Solution

rdov earned 100 total points
ID: 6234258
You can try something like this :

    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;MSIE 5.01; Windows NT 5.0)");

LVL 14

Expert Comment

ID: 8950879

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

--  points to rdov

Please leave any comments here within the next seven days.
EE Cleanup Volunteer
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 104
Java - Why doesn't this JFrame work 3 58
Android development question 2 34
Java: anonymous class 4 16
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now