Results 1 to 3 of 3

Thread: Web page junk [resolved]

  1. #1

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935

    Web page junk [resolved]

    How can I get the resulting HTML source of some URL, like http://www.cnn.com/ ? I'm sure some classes in java.net are used but none strike me immediately.
    Last edited by filburt1; Dec 13th, 2001 at 10:01 PM.

  2. #2
    Member
    Join Date
    Nov 2001
    Location
    C2C
    Posts
    42
    URL class. I remember there are openConncetion and openstream methods.
    Hai Kuo Ren Yu Yao. Tian Gao Ren Niao Fei.

  3. #3

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    After scrounging Google (It was hard to get good search terms) I made this which appears to work fine:
    Code:
    package com.turtletips;
    
    import java.net.URL;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.BufferedReader;
    
    public class HTMLDownloader
    {
        public static String getHTMLFromURL(URL u) throws Exception
        {
            try
            {
                StringBuffer cache = new StringBuffer();
                InputStream in = u.openStream();
                int currentByte = in.read();
                while (currentByte != -1)
                {
                    cache.append((char)currentByte);
                    currentByte = in.read();
                }
                return new String(cache);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    
        public static void main(String[] args)
        {
            try
            {
                System.out.println(HTMLDownloader.getHTMLFromURL(
                        new URL("http://www.sunspot.net/wireless/avantgo/")));
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width