以下是用于读取网页html内容的代码。
使用Java读取网页不同于使用Java调用IE打开网页。普通博客的访问者数量不能以这种方式增加。为了增加访问者人数,您需要一个可以调用IE来打开网页的程序。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
public static void main(String[] args) {
try {
URL google = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(google.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
// Process each line.
System.out.println(inputLine);
}
in.close();
} catch (MalformedURLException me) {
System.out.println(me);
} catch (IOException ioe) {
System.out.println(ioe);
}
}//end main
}
这实际上是最底层的。我们可以简单地使用Jsoup以更简单的方式完成相同的事情。