通过网站域名获取该网站的源码

来源:互联网 发布:破解路由器密码的软件 编辑:程序博客网 时间:2024/05/17 17:56
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();  
            }  
        }  
  
    }  

}
0 0