(一)URL对象

本文详细介绍了网络编程的基础知识,包括IPv4和IPv6地址的区别,主机名、端口号和服务类型的定义,以及如何使用Java通过URL读取WWW信息。此外,还深入讲解了URL对象的创建方法及其属性获取方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       Ipv4地址(32位,4个字节)
       Ipv6地址(128位,16个字节)
       主机名(hostname)如:www.tsinghua.edu.cn
       端口号(postnumber)如:80,21,23,1-1024为保留端口号
       服务类型(service)如:http,telnet,ftp,smtp

  • 通过URL读取www信息

       Ipv4地址(32位,4个字节)
       Ipv6地址(128位,16个字节)
       主机名(hostname)如:www.tsinghua.edu.cn
       端口号(postnumber)如:80,21,23,1-1024为保留端口号
       服务类型(service)如:http,telnet,ftp,smtp

public class URLReader{
   public static void main(String[] args)throws Exception{
   URL cs=new URL("http://www.sina.com/");
   BufferedReader in=new BufferedReader(new InputStreamReader(cs.openStream()));
   String inputLine;
   while((inputLine=in.readLine())!=null)
      System.out.println(inputLine);
   in.close();   
  }
}
  • 构造URL对象

public URL(String spec)
URL urlBase=new URL("http://www.gamelan.com/");

public URL(URL context,String spec)
URL gamelanGames=new URL(gamelan,"Gamelan.game.html");

public URL(String protocol,String host,String file);
URL gamelan=new URL("http","www.gamelan.com","/pages/Gamelan.net.html");

public URL(String protocol,String host,int port,String file);

URL gamelan=new URL("http","www.gamelan.com",80,"pages/Gamelan.network.html");
  • 获取URL对象属性

public String getProtocol()
public String getHost()
public String getport()
public String getFile()
public String getRef()

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值