package laa;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HtmlInfo {
/**
* @author LIANAN
* @param url
* @param encoding
* @return
* @throws Exception
*/
public static String getHtml(String url,String encoding)throws Exception{
StringBuilder sb=new StringBuilder();URL u=new URL(url);HttpURLConnection con = (HttpURLConnection)u.openConnection();//设置
con.setRequestProperty("User-Agent", "MSIE 7.0");BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream(),encoding));String s=null;while((s=br.readLine())!=null){sb.append(s);sb.append("\r\n");}con.disconnect();return sb.toString();}
public static void main(String[] arg0)throws Exception{
System.out.println(getHtml("http://www.baidu.com","gbk"));}}
--转自
该贴由koei123转至本版2015-7-14 11:14:20