利用java.net包访问网络,连接网页并返回html页面代码

package org.kevin;


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);
}
}

你可能感兴趣的:(java,html,.net)