Java网络编程(URL&URLConnection)

 1 package cn.itcast.net.p2.ie_server;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.net.URL;
 6 import java.net.URLConnection;
 7 
 8 public class URLDemo {
 9 
10     /**
11      * @param args
12      * @throws IOException 
13      */
14     public static void main(String[] args) throws IOException {
15 
16         String str_url = "http://192.168.1.100:8080/myweb/1.html";
17         
18         URL url = new URL(str_url);
19         
20 //        System.out.println("getProtocol:"+url.getProtocol());
21 //        System.out.println("getHost:"+url.getHost());
22 //        System.out.println("getPort:"+url.getPort());
23 //        System.out.println("getFile:"+url.getFile());
24 //        System.out.println("getPath:"+url.getPath());
25 //        System.out.println("getQuery:"+url.getQuery());
26         
27 //        InputStream in = url.openStream();//这种方式等价于下面这种方式
28         
29         //获取url对象的Url连接器对象。将连接封装成了对象:java中内置的可以解析的具体协议的对象+socket.
30         URLConnection conn = url.openConnection();
31         
32 //        String value = conn.getHeaderField("Content-Type");
33 //        System.out.println(value);
34         
35 //        System.out.println(conn);
36         //sun.net.www.protocol.http.HttpURLConnection:http://192.168.1.100:8080/myweb/1.html
37         
38         InputStream in = conn.getInputStream();
39         
40         byte[] buf = new byte[1024];
41         int len = in.read(buf);
42         
43         String text = new String(buf,0,len);
44         
45         System.out.println(text);
46         
47         in.close();
48         
49         
50         
51         
52         
53     }
54 
55 }

 


本文为博主原创文章,转载请注明出处:http://www.cnblogs.com/ysw-go/
1、本博客的原创原创文章,都是本人平时学习所做的笔记,如有错误,欢迎指正。
2、如有侵犯您的知识产权和版权问题,请通知本人,本人会即时做出处理文章。
3、本博客的目的是知识交流所用,转载自其它博客或网站,作为自己的参考资料的,感谢这些文章的原创人员

posted @ 2016-03-28 20:19  菜鸟奋斗史  阅读(1109)  评论(0编辑  收藏  举报