首页 » Program » Java » 正文

Java URL class

在java.net包中定义了URL类,该类用来处理有关URL的内容。

openStream()方法只能读取网络资源,若要既能读取又能发送数据,则要用到URL类的openConnection()方法来创建一个 URLConnection类的对象,此对象在本地机和URL指定的远程节点建立一条HTTP协议的数据通道,可进行双向数据传输

import java.io.BufferedReader;
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.net.URL;  
import java.net.URLConnection; 

public class Urltest
{
    public static void main(String[] args) throws IOException
    {
         URL baidu = new URL("http://www.baidu.com");
         System.out.println("protocol: "+baidu.getProtocol());
         System.out.println("authority: "+baidu.getAuthority());
         System.out.println("host: "+baidu.getHost());
         System.out.println("port: "+baidu.getPort());
         System.out.println("path: "+baidu.getPath());
         System.out.println("query: "+baidu.getQuery());
         System.out.println("filename: "+baidu.getFile());
         System.out.println("ref: "+baidu.getRef());

          /*URLConnection baiduconn = baidu.openConnection();
           in = new BufferedReader(new InputStreamReader(baiduconn.getInputStream()));
         */
         /*BufferedReader in = new BufferedReader(new InputStreamReader(baidu.openStream()));
         String inputline;
         while((inputline = in.readLine())!=null)
             System.out.println(inputline);*/
         /*in.close();*/

     }
}

参考链接:

http://www.runoob.com/java/java-url-processing.html

http://blog.csdn.net/witsmakemen/article/details/6989010

 

Comment