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);
   }

 }

}

發佈了87 篇原創文章 · 獲贊 34 · 訪問量 48萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章