Go Premium for a chance to win a PS4. Enter to Win


How to emulate MSIE or Netscape in URLConnection?

Posted on 2001-06-27
Medium Priority
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("www.test.com");
   InputStream is = uc.getInputStream();
   int b = is.read();
   String pagestr = new String();
   while (b != -1){
      pagestr += /* concatenate all the read bytes */
      b = is.read();

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 java.sun.com
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 400 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

876 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