package com.wallen.test;
import java.io.*;
import java.net.URL;
/**
* 通过网站域名获取该网站的源码
* @author Wallen
*
*/
public class Test2 {
public static void main(String[] args) {
Test2 t = new Test2();
t.getHTMLSrc("http://www.sina.com");
}
public void getHTMLSrc(String url){
InputStream openStream = null;
BufferedReader buf = null;
try {
String line = null;
URL theUrl= new URL(url);
openStream = theUrl.openStream();
/<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
//构建输入流的的字符集必须和HTML源码中的 charset一致
buf = new BufferedReader(new InputStreamReader(openStream,"utf-8"));
while((line = buf.readLine()) != null){
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(openStream!=null){
openStream.close();
}
if(buf!=null){
buf.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.*;
import java.net.URL;
/**
* 通过网站域名获取该网站的源码
* @author Wallen
*
*/
public class Test2 {
public static void main(String[] args) {
Test2 t = new Test2();
t.getHTMLSrc("http://www.sina.com");
}
public void getHTMLSrc(String url){
InputStream openStream = null;
BufferedReader buf = null;
try {
String line = null;
URL theUrl= new URL(url);
openStream = theUrl.openStream();
/<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
//构建输入流的的字符集必须和HTML源码中的 charset一致
buf = new BufferedReader(new InputStreamReader(openStream,"utf-8"));
while((line = buf.readLine()) != null){
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(openStream!=null){
openStream.close();
}
if(buf!=null){
buf.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}