1 import java.io.BufferedReader;
2 import java.io.InputStreamReader;
3 import java.net.HttpURLConnection;
4 import java.net.URL;
5
6 public class URLUtil {
7
8 public static String getHtml(String urlString) {
9 try {
10 StringBuffer html = new StringBuffer();
11 URL url = new URL(urlString);
12 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
13 InputStreamReader isr = new InputStreamReader(conn.getInputStream());
14 BufferedReader br = new BufferedReader(isr);
15 String temp;
16 while ((temp = br.readLine()) != null) {
17 html.append(temp).append("\n");
18 }
19 br.close();
20 isr.close();
21 return html.toString();
22 } catch (Exception e) {
23 e.printStackTrace();
24 return null;
25 }
26 }
27
28 public static void main(String[] args) {
29 System.out.println(URLUtil.getHtml("http://www.163.com"));
30 }
31 }
posted on 2009-04-29 08:06
lameer 阅读(988)
评论(0) 编辑 收藏