Java网络编程URL和URI




获得URL的方法 
Java代码   收藏代码
  1. URI.toURL()  
  2. File.toURL()  
  3. ClassLoader.getSystemResource(String name)  
  4. Applet.getDocumentBase()  



URL有以下5部分组成 
http://www.ibiblio.org/javafaq/books/jnp/index.html?isbn=12345#toc 
协议,也称模式 http 
授权机构 www.ibiblio.org 
路径 javafaq/books/jnp/index.html 
查询字符串 isbn=12345 
片段标识符,也成为段或ref toc 
授权机构可被进一步分为用户信息(包含口令)、主机、端口 
admin:pwd@www.blackstar.com:8080 

URL编码解码 
URLEncoder不会去判断这些字符如何用于URL,比如 = & 表示查询参数,因此必须将URL逐部分进行编码 
URLDecoder不涉及非转义字符,所以可以传递整个URL,而不用将其分解。 
Java代码   收藏代码
  1. public class URLEncoderExample {  
  2.   public static void main(String[] args) throws UnsupportedEncodingException {  
  3.     String input = "http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&stype=stext&q=+\"Java Network Programming\"";  
  4.     System.out.println(input);  
  5.     String output = URLEncoder.encode(input, "utf-8");  
  6.     System.out.println(output);  
  7.     String s = URLEncoder.encode("http""utf-8");  
  8.     s += "://";  
  9.     s += URLEncoder.encode("www.altavista.com""utf-8");  
  10.     s += "/";  
  11.     s += URLEncoder.encode("cgi-bin""utf-8");  
  12.     s += "/";  
  13.     s += URLEncoder.encode("query""utf-8");  
  14.     s += "?";  
  15.     s += URLEncoder.encode("pg""utf-8");  
  16.     s += "=";  
  17.     s += URLEncoder.encode("q""utf-8");  
  18.     s += "&";  
  19.     s += URLEncoder.encode("kl""utf-8");  
  20.     s += "=";  
  21.     s += URLEncoder.encode("XX""utf-8");  
  22.     s += "&";  
  23.     s += URLEncoder.encode("stype""utf-8");  
  24.     s += "=";  
  25.     s += URLEncoder.encode("stext""utf-8");  
  26.     s += "&";  
  27.     s += URLEncoder.encode("q""utf-8");  
  28.     s += "=";  
  29.     s += URLEncoder.encode("\"Java Network Programming\"""utf-8");  
  30.     System.out.println(s);  
  31.   }  
  32. }  



URL和URI 
URL对象是从网络获取的应用程序协议的表示,而URI对象纯粹是可以解析和操作的字符串。 
URI没有网络获取功能,URL有字符串解析方法getFile() getRef(),处理URL返回的字符串,分解为各个部分。 
Java代码   收藏代码
  1. // URLSplitter  
  2. try {  
  3.   URL u = new URL("http://xace:pwd@www.ibiblio.org/javafaq/books/jnp/index.html?isbn=12345#toc");  
  4.   System.out.println("The URL is " + u);  
  5.   System.out.println("The scheme/protocol is " + u.getProtocol());  
  6.   System.out.println("The user info is " + u.getUserInfo());  
  7.   String host = u.getHost();  
  8.   if (host != null) {  
  9.     int atSign = host.indexOf('@');  
  10.     if (atSign != -1)  
  11.       host = host.substring(atSign + 1);  
  12.     System.out.println("The host is " + host);  
  13.   } else {  
  14.     System.out.println("The host is null.");  
  15.   }  
  16.   System.out.println("The port is " + u.getPort());  
  17.   System.out.println("The defaultPort is " + u.getDefaultPort());  
  18.   System.out.println("The path is " + u.getPath());  
  19.   System.out.println("The file is " + u.getFile());  
  20.   System.out.println("The ref is " + u.getRef());  
  21.   System.out.println("The query string is " + u.getQuery());  
  22.   System.out.println("The authority is:" + u.getAuthority());  
  23.     } catch (MalformedURLException ex) {  
  24.   System.err.println("is not a URL I understand.");  
  25. }  
  26.   
  27. // URISplitter  
  28. try {  
  29.   URI u = new URI("http://xace:pwd@www.ibiblio.org/javafaq/books/jnp/index.html?isbn=12345#toc");  
  30.   System.out.println("The URI is " + u);  
  31.   if (u.isOpaque()) {  
  32.     System.out.println("This is an opaque URI.");  
  33.     System.out.println("The scheme is " + u.getScheme());  
  34.     System.out.println("The scheme specific part is " + u.getSchemeSpecificPart());  
  35.     System.out.println("The fragment ID is " + u.getFragment());  
  36.   } else {  
  37.     System.out.println("This is a hierarchical URI.");  
  38.     System.out.println("The scheme is " + u.getScheme());  
  39.     try {  
  40.       u = u.parseServerAuthority();  
  41.       System.out.println("The host is " + u.getUserInfo());  
  42.       System.out.println("The user info is " + u.getUserInfo());  
  43.       System.out.println("The port is " + u.getPort());  
  44.     } catch (URISyntaxException ex) {  
  45.       System.out.println("The authority is " + u.getAuthority());  
  46.     }  
  47.     System.out.println("The path is " + u.getPath());  
  48.     System.out.println("The query string is " + u.getQuery());  
  49.     System.out.println("The fragment ID is " + u.getFragment());  
  50.   }   
  51. catch (URISyntaxException ex) {  
  52.   System.err.println(" does not seem to be a URI.");  
  53. }  


URI的各部分 
模式、模式的有部分、片段标识符 
scheme:scheme-specific-part:fragment 

代理 
设置HTTP代理 
Java代码   收藏代码
  1. // java.oreilly.com和xml.oreilly.com,不使用代理  
  2. System.setProperty("http.proxyHost""192.168.5.1");  
  3. System.setProperty("http.proxyPort""9000");  
  4. System.setProperty("http.nonProxyHosts""java.oreilly.com|xml.oreilly.com");  

java -Dhttp.proxyHost=192.168.5.1 -Dhttp.proxyPort=9000 -Dhttp.nonProxyHosts=*.oreilly.com aaa.bbb.Class 
相应的有FTP代理和Socks代理,但是Socks代理不能设置nonProxyHosts 

Proxy类 ProxySelector类 

cookie 
Java1.5之前,只能通过直接操作HTTP首部来设置cookie, 
Set-Cookie: user=xace 
新版本的cookie规范,在“name=value”对后,需要一个版本属性,还允许将cookie值引号引起来,这样在引号中可以包含空格 
Set-Cookie2: food="chocolate ice cream"; Version=1 
当相同服务器请求同一文档时,客户端在发送给服务器的请求的Cookie首部字段中回显次cookie, 
Cookie: user=xace 
Cookie: $Version=1; food="chocolate ice cream" 
客户端的任务就是记住收到的所有cookie,在适当的时候将正确的cookie发送给最初的服务器。 
但是这有些复杂,因为cookie有一些表示属性 
过期时间 Expires=Wed, 21-Dec-2010 15:23:00 GMT Max-Age=3600 
路径 Path=/blog 
域 Domain=.iteye.com 
端口 Port="80 8080" 
安全性选项 secure 
这些属性往服务器回传时,均要加上$Expires $Path 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值