zoukankan      html  css  js  c++  java
  • 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、本博客的目的是知识交流所用,转载自其它博客或网站,作为自己的参考资料的,感谢这些文章的原创人员

  • 相关阅读:
    Fedora 8 三维特效美化全攻略
    用C++编写简单绘图语言的语法分析器
    linux tar 的使用
    jquery基础
    hibernate4中主要的配置文件配置
    在对List集合进行remove()等操作重写equals()和hashCode()方法的必要性
    jquery基础2
    javascript时间格式化
    linux之shell编程shell基础
    html会移动的文字
  • 原文地址:https://www.cnblogs.com/ysw-go/p/5330426.html
Copyright © 2011-2022 走看看