第二部分 用url检索数据
一.url类
java程序定位和检索网络上的数据最简单的方法是使用url类。
java.net.url类是对统一资源定位符的抽象。url对象建立后,它的字段就不再改变。
构造java.net.url实例的六个构造器:
1) 用字符串构造url
public url(string url) throws malformedurlexception
2)用组件构造url
public url(string protocol,string hostname,string file) throws malformedurlexception
这个构造器将端口设置为-1,所以协议的所有默认端口都可以用。
3)用组件构造url
public url(string protocol,string host,int port,string file) throws malformedurlexception
对于默认端口不正确的极少见情况,这个构造器可以明确地用int变量指定端口
4) 构造相对url
public url(url base,string relative) throws malformedurlexception
这个构造器根据相对url和基本url构造绝对url
例如:
try
{
url u1=new url(“http://metalab.unc.edu/javafaq/index.html”);
url u2=new url(u1,”mailinglists.html”);
}
catch (malformedurlexception e)
{
system.err.println(e);
}
5) 指定urlstreamhandler
public url(url base,string relative,urlstreamhandler handler) throws malformedurlexception
这个构造器由一个基本url和相对part构建一个相对url,然后用指定的处理器处理url
6) 指定urlstreamhandler
public url(string protocol,string host,int port,string file,urlstreamhandler handler) throws malformedurlexception
这个构造器从它的组件部分构建url,然后用指定的处理器处理url。
除了这些构造器,java类库中还有许多其他方法返回url对象。其中大多数只是简单的获取方法,只返回用户可能已经知道的url,因为用户已经首先用它来创建对象。
二.分解url
url可以认为是由五部分组成的:
1) 策略(scheme),也可以认为是协议
2) 权限
权限可以进一步分为用户信息、主机和端口。
3) 路径
4) 参考(ref),也称为节(section)或者已命名锚(named anchor)
5) 查询字符串
以只读方式访问url这五部分的公共方法:
1) public string getprotocol()
getprotocol()方法返回一个string,包含url的策略:比如:”http”,”https”,”file”等等。
2) public string gethost()
gethost()方法返回一个string,包含url的主机名。
3) public int getport()
getport()方法返回端口号,该数值是一个在url中指定的int。如果在url中没有指定端口,那么getport()方法返回-1,标志着url没有明确指定端口,同时使用协议的默认端口。
4) public string getfile()
getfile()方法返回一个string,其中包含url的路径和文件部分。从主机名后的第一个“/”开始,到另起一部分的“#”之前的符号,全部认为是文件部分。
5) public string getpath()
它与getfile()方法意义相同。
6) public string getref()
getref()返回url的命名锚部分。如果url没有一个已命名的锚,那么这个方法返回null。
例如:
try
{
url u=new url(“http://metalab.unc.edu/javafaq/javafaq.html#xtocid1902914”);
system.out.println(“the ref of ”+u+ “ is ”+u.getref();
}
catch (malformedurlexception e)
{
system.err.println(e);
}
这段代码返回中,getref()返回的字符串xtocid1902914
7) public string getquery()
getquery()方法返回url的查询字符串。如果url没有查询字符串,那么这个方法返回null。
8)public string getuserinfo()
某些url包含用户名,并且甚至包含密码信息。它位于策略之后和主机名之前,“@”符号划定它的范围。如果url不包含任何用户信息,那么这个方法返回null。mailto url会出现例外,在mailto:elharo@metalab.unc.edu 这样的url中,elharo@metalab.unc.edu是路径,而不是用户信息和主机名。
9)public string getauthority()
getauthority()返回url的权限。包括用户信息、主机名和端口。
三.从url检索数据
1) public final inputstream openstream() throws ioexception
openstream()方法连接url参考的资源,实现客户机和服务器之间所有必要的握手连接,并且从可读数据返回inputstream。从inputstream得到的数据是url参考文件的原始(即没有解析过的)内容。
2) public urlconnection openconnection() throws ioexception
openconnection()方法打开一个到指定url的套接字并且返回一个urlconnection对象。urlconnection代表一个到网络资源的开放连接。
3) public final object getcontent() throws ioexception
getcontent()方法检索url参考的数据,并且试图把它转换成对象的某个类型。
4) public final object getcontent(class[] classes) throws ioexception
getcontent()方法的重载变量使得可以选择我们想要的类作为返回内容,这个方法试图按数组中的顺序返回url的内容。
例如:
url u=new url(“http://nwu.org”);
class[] types=new class[3];
types[0]=string.class;
types[1]=reader.class;
types[2]=inputstream.class;
object o=u.getcontent(types);
然后必须用instanceof测试返回对象的类型
if (o instanceof string)
{
system.out.println(o);
}
else if (o instanceof reader)
{
int c;
reader r=(reader) c;
while ((c=r.read()) != -1)
system.out.print((char) c);
}
else if (o instanceof inputstream)
{
int c;
inputstream in=(inputstream) o;
while ((c=in.read()) != -1)
system.out.write(c);
}
else
{
system.out.println(“error:unexcepted type ”+o.getclass());
}
四.工具方法
1) public boolean samefile(url other)
samefile()方法测试测试两个url对象是否指向同一个文件。samefile()执行的检测非常肤浅,它只是比较相对立的字段是否相等。samefile()和equals()有相似之处,差别在于equals()需要考虑所有任何参数,而samefile()不考虑。此外,任何类都可以传递到equals(),却只有url类能传递到samefile()。
2) public string toexternalform()
toexternalform()和tostring()方法相同。
五.对象方法
url是从java.lang.object继承来的,所以可以访问object类的全部方法。类似于ientaddress类的对象方法。
六.协议处理器方法
1) public static synchronized void seturlstreamhandlerfactory(urlstreamhandlefactory factory)
这个方法为应用程序设置urlstreamhandlefactory,如果已经设置了类库,则此方法触发一个通用error。
2) public void set(string protocol,string host,int port,string authority,string userinfo,string path,string query,string ref)
七.urlencoder和urldecoder类
java.net.urlencoder类包含一个叫encode()的单一静态方法
public static string encode(string s)
urlencoder.encoder()把任何非文字数字字符(除了空格、下划线、连字号、句点和星号)都转换位%序列,空格被转换为加号。
urlencoder的主要用途在于为使用get的cgi程序的通信准备查询字符串。
urldecoder类把所有的加号转换位空格,把所有的百分号转义符转换为相应的字符。
public static string decode(string s) throws exception
如果字符串包含一个百分号,而其后没有两个十六进制的数字,那么就会触发一个illegalargumentexception。这个方法传递的是非转义字符,所以用户可以传递一个完整的url。
