网络编程 URL
1 package netTest; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.net.MalformedURLException; 6 import java.net.URL; 7 8 public class URLConnection { 9 public static void main(String[] args) throws IOException { 10 URL url = new URL("http://localhost:8080/myweb/demo.html"); 11 // 内部帮忙做连接 不用再写socket了,而且是带着协议封装的。此时在应用层 12 java.net.URLConnection conn = url.openConnection(); 13 //.out.println(conn); 14 //socket 的inputStream 流 15 InputStream in = conn.getInputStream(); 16 byte[] buf = new byte[1024]; 17 int len = in.read(buf); 18 System.out.println(new String(buf,0,len)); 19 } 20 }