- 本文转自:http://weizhigan.iteye.com/blog/903251,如有侵权请告知!
package org.kevin;
//自己ctrl+shift+F格式化对其哈!(真是良心博主)
import java.net.*;
import java.io.*;
public class TestURL
{
public static String getURL(String s)
{
BufferedReader br=null;
URL url=null;
URLConnection con;
StringBuffer ret = new StringBuffer();
DataInputStream dis;
try
{
url=new URL(s);
// con = url.openConnection();
// con.connect();
// dis = new DataInputStream(con.getInputStream());
br=new BufferedReader(new InputStreamReader(url.openStream()));//openStream()是为了获得一个inputstream
String html="";
while((html=br.readLine())!=null)
{
ret.append(html+"\r\n");
}
br.close();
}catch(Exception e)
{
e.printStackTrace();
}
return ret.toString();
}
public static void main(String[] args)
{
String s = getURL("http://www.baidu.com");
System.out.println(s);
}
}