Click to See Complete Forum and Search --> : Web page junk [resolved]
filburt1
Dec 13th, 2001, 07:48 PM
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.
e-mulan
Dec 13th, 2001, 08:56 PM
URL class. I remember there are openConncetion and openstream methods.
filburt1
Dec 13th, 2001, 09:01 PM
After scrounging Google (It was hard to get good search terms) I made this which appears to work fine:
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();
}
}
}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.