Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: oracle Reading Directly from a URL code not working properly

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default oracle Reading Directly from a URL code not working properly

    I'm trying to learn how to download images and files from a website trough a java program. The code below is copied from Reading Directly from a URL (The Java™ Tutorials > Custom Networking > Working with URLs). This program is supposed to display the html file code from the url provided.

    Quoted from the site:"When you run the program, you should see, scrolling by in your command window, the HTML commands and textual content from the HTML file located at Oracle | Hardware and Software, Engineered to Work Together. "

    My problem is that it works for some websites, but not for interfacelift.com. It doesn't display anything for that website. I'm trying to figure out why.
    import java.net.*;
    import java.io.*;
     
    public class URLReader {
    public static void main(String[] args) throws Exception {
     
        URL oracle = new URL("http://interfacelift.com/");
        BufferedReader in = new BufferedReader(
        new InputStreamReader(oracle.openStream()));
     
        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,378
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: oracle Reading Directly from a URL code not working properly

    The site might not respond to non-browser accesses. What do you get in a browser?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: oracle Reading Directly from a URL code not working properly

    Quote Originally Posted by Norm View Post
    The site might not respond to non-browser accesses. What do you get in a browser?
    It opens the website like it should.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,378
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: oracle Reading Directly from a URL code not working properly

    Try building a HTTP request header that will make the site want to respond to your request.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    reapaz1 (February 12th, 2013)

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: oracle Reading Directly from a URL code not working properly

    Yes that was it!!! Thank you.

    My working code:
    import java.io.IOException;
    import java.net.*;
    import java.io.*;
     
     
    public class TestUrlOpener {
     
        public static void main(String[] args) throws IOException {
            URL url = new URL("http://interfacelift.com");
            URLConnection hc = url.openConnection();
            hc.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");
    BufferedReader in = new BufferedReader(new InputStreamReader(
                                        hc.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) 
                System.out.println(inputLine);
            in.close();
        }
    }
    Last edited by Norm; February 9th, 2013 at 04:10 PM. Reason: added code tags

Similar Threads

  1. Applet not working properly
    By kookevk in forum AWT / Java Swing
    Replies: 1
    Last Post: February 3rd, 2012, 12:29 AM
  2. issue with reading a clob variable from oracle
    By computerbum in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 28th, 2010, 06:08 PM
  3. repeatedly reading from a url? anyone?
    By tandren544 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 15th, 2010, 04:34 PM