一、用JAVA實(shí)現(xiàn)URL
在JAVA中,Java.net包里面的類是進(jìn)行網(wǎng)絡(luò)編程的,其中java.net.URL類和java.net.URLConection類使編程者方便地利用URL在Internet上進(jìn)行網(wǎng)絡(luò)通信。1、創(chuàng)建URL對(duì)象 URL類有多種形式的構(gòu)造函數(shù):
(1) URL ( String url)
url代表一個(gè)絕對(duì)地址,URL對(duì)象直接指向這個(gè)資源,如:
URL urll=new URL(http://www.cqwu.edu.cn);
(2) URL ( URL baseURL , String relativeURL)
其中,baseURL代表絕對(duì)地址,relativeURL代表相對(duì)地址。如:
URL urll=new URL(http://www.cqwu.edu.cn);
URL lib=new URL(urll , "library / library.asp");
(3) URL ( String protocol , String host , String file)
其中,protocol代表通信協(xié)議,host代表主機(jī)名,file代表文件名。如:
new URL ("http" , www.cqwu.edu.cn, "/ test / test.asp");
(4) URL ( String protocol , String host , int port , String file)
URL lib = new URL ("http" , www.cqwu.edu.cn, 80 , "/ test / test.asp");2、獲取URL對(duì)象的屬性 getDefaultPort(): 返回默認(rèn)的端口號(hào)。
getFile(): 獲得URL指定資源的完整文件名。
getHost(): 返回主機(jī)名。
getPath(): 返回指定資源的文件目錄和文件名。
getPort(): 返回端口號(hào),默認(rèn)為-1。
getProtocol(): 返回表示URL中協(xié)議的字符串對(duì)象。
getRef(): 返回URL中的HTML文檔標(biāo)記,即#號(hào)標(biāo)記。
getUserInfo: 返回用戶信息。
toString: 返回完整的URL字符串。二、Internet尋址 java.net包可以用32位int形式來操作32位的IP地址(即Internet主機(jī)地址)。類InetAddress實(shí)際上是可以把Internet地址換算成代表該地址的對(duì)象。Java就是靠這個(gè)類來顯示Internet地址已經(jīng)相關(guān)信息的。
InetAddress有以下常用方法:
getAddress(): 返回IP地址的字節(jié)形式。
getAllByName(): 返回指定主機(jī)名的IP地址。
getbyAddress(): 返回指定字節(jié)數(shù)組的IP地址形式。
getByName(): 返回指定主機(jī)名的IP地址對(duì)象。
getHostAddress(): 返回主機(jī)地址的字符串形式。
getLocalHost(): 返回當(dāng)前主機(jī)名。
hastCode(): 返回InetAddress對(duì)象的哈希碼。
toString: 返回地址轉(zhuǎn)換成的字符串。
InetAddress類沒有提供返回構(gòu)造函數(shù),所以不能用new()方法來創(chuàng)建它的對(duì)象,而只可以調(diào)用靜態(tài)方法getLocalHost()、getByName()、getByAddress()等來生成InetAddress類的實(shí)質(zhì)。
程序代碼
import java.net.*;
import java.io.*;
public class InetAddDemo //extends Applet
{
?public void testOperate()
?{
??try
??{
???InetAddress address=InetAddress.getLocalHost();
???log("本機(jī)地址字符串:"+address.getHostAddress());
???log("本機(jī)主機(jī)名:"+address.getHostName());
???log("本機(jī)主機(jī)名:"+address.getLocalHost());
???log("哈希碼:"+address.hashCode());
???byte b[]=address.getAddress();
???System.out.println("字符形式:"+b);
???log("地址字符串:"+address.toString());
??}
??catch(Exception e)
??{
???//e.printStackTrace("不能打開這個(gè)URL");
??}
?}
?
?public void log(String strInfo)
?{
??System.out.println(strInfo);
?}
?
?public static void main(String args[])
?{
??InetAddDemo IAdd=new InetAddDemo();
??IAdd.testOperate();
?}
}
有兩種方法可以用來訪問Internet。一是利用URL類的openStream()方法;二是使用openConnection()方法創(chuàng)建一個(gè)URLConnection類的對(duì)象。
其中,方法openStream()與指定的URL建立連接并返回InputStream類的對(duì)象,以從這一連接中讀取數(shù)據(jù)。
程序代碼
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);
?? }
?}
}
上例首先創(chuàng)建URL對(duì)象url,并在其基礎(chǔ)上打開輸入流獲取InputStreamReader對(duì)象,再由此對(duì)象創(chuàng)建BufferedReader對(duì)象br,從br中讀取數(shù)據(jù)即可得到url所指定的資源文件。
上面的openStream()方法只能讀取網(wǎng)絡(luò)資源,若要既能讀取又能發(fā)送數(shù)據(jù),則要用到URL類的openConnection()方法來創(chuàng)建一個(gè)
URLConnection類的對(duì)象,此對(duì)象在本地機(jī)和URL指定的遠(yuǎn)程節(jié)點(diǎn)建立一條HTTP協(xié)議的數(shù)據(jù)通道,可進(jìn)行雙向數(shù)據(jù)傳輸。
類URLConnection提供了很多設(shè)置和獲取連接參數(shù)的方法,最常用到的是getInputStream()和getOutputStream()方法,如: URL sum=new URL("
http://java.sum.com/cgi-bin/backwards");
URLConnection suncon=buaa.openConnection();
sumcon.setDoOutput(true);
DataInputStream dis=new DataInputStream(suncon.getInputStream());
PrintStream ps=new PrintStream(suncon.getOutputStream());
String str=dis.readLine();
ps.println("來自客戶機(jī)的信息:.......");
posted on 2006-04-03 15:15
SIMONE 閱讀(653)
評(píng)論(0) 編輯 收藏 所屬分類:
JAVA