URL URLConnection

一、URL类: 
类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询。 
URL(String spec)  
            根据 String 表示形式创建 URL 对象。 
URL(String protocol, String host, int port, String file)  

            根据指定 protocol、host、port 号和 file 创建 URL 对象。

 getFile()                         获

           得此 URL 的文件名。  

String getHost()            

          获得此 URL 的主机名(如果适用)。 

String getPath()            

         获得此 URL 的路径部分。 

int getPort()                   

        获得此 URL 的端口号。

String getProtocol()        

       获得此 URL 的协议名称。  

openConnection() 返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。

 URL示例代码: 

import java.net.*; public class URLDemo { 

      public static void main(String[] args) { 

//          URLDemo urldemo = new URLDemo();               

try { 

                  URL hp=new URL("http://www.hao123.com/"); 

                  System.out.println("Protocol: "+hp.getProtocol());                   

System.out.println("Port: "+hp.getPort());               

    System.out.println("Host: "+hp.getHost());                

   System.out.println("File: "+hp.getFile()); 

                  System.out.println("Ext: "+hp.toExternalForm());         

      } catch (MalformedURLException ex) {    

               System.out.println(ex.toString());               

}       } } 

输出:

Protocol: http 

Port: -1 

Host: www.hao123.com 

File: / 

Ext: http://www.hao123.com/  
二、URLConnection类 
抽象类 URLConnection 是所有类的超类,它代表应用程序和 URL 之间的通信链接。此类的实例可用于读取和写入此 URL 引用的资源。通常,创建一个到 URL 的连接需要几个步骤:  
openConnection() 
connect() 
对影响到远程资源连接的参数进行操作。 与资源交互;查询头字段和内容。 
---------------------------->  
时间 

1. 通过在 URL 上调用 openConnection 方法创建连接对象。 

 2. 操作设置参数和一般请求属性。  

3. 使用 connect 方法建立到远程对象的实际连接。  

4. 远程对象变为可用。远程对象的头字段和内容变为可访问。


有两种方法可以用来访问Internet。一是利用URL类的openStream()方法;二是使用openConnection()方法创建一个URLConnection类的对象。
  其中,方法openStream()与指定的URL建立连接并返回InputStream类的对象,以从这一连接中读取数据。


  import java.net.*; import java.io.*; 

import java.util.Date; 
public class URLConnectionDemo { 

     public static void main(String[] args) throws Exception{

 //         URLConnectionDemo urlconnectiondemo = new URLConnectionDemo();         

     int c; 

             URL hp=new URL("http://www.hao123.com/");      

        URLConnection hpCon=hp.openConnection(); 

             System.out.println("Date: "+new Date(hpCon.getDate()));      

        System.out.println("Content-Type: "+hpCon.getContentType()); 

             System.out.println("Expires: "+hpCon.getExpiration());              

System.out.println("Last-Modified: "+ new Date(hpCon.getLastModified())); 

             int len=hpCon.getContentLength(); 

             System.out.println("Content-Length: "+len);            

  if (len>0) { 

                 System.out.println("=== content ===");         

         InputStream input=hpCon.getInputStream();         

         int i=len; 

                 while ((c=input.read())!=-1 && (--i>0)) {          

            System.out.print((char)c); 

                 }             

 } 

               else 

                   System.out.println("No Content Available!");      

        }                }


 运行结果: 

Date: Mon Aug 27 11:07:02 CST 2007 Content-Type: text/html Expires: 1188443222000 
Last-Modified: Fri Aug 24 12:29:00 CST 2007 Content-Length: 44966 === content === 
<html><head> 

<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 

.......




import java.net.*;
import java.io.*;

public class ReadURL
{
 
public static void main(String args[]) throws Exception
 
{
  
try
  
{
   URL url
=new URL("http://www.baidu.com");
   InputStreamReader isr
=new InputStreamReader(url.openStream());
   BufferedReader br
=new BufferedReader(isr);
  
   String str;
   
while((str=br.readLine())!=null)
   
{
    System.out.println(str);
   }

  
   br.close();
   isr.close();
   }

   
catch(Exception e)
   
{
    System.out.println(e);
   }

 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值