Java网络编程URL和URI

[b]获得URL的方法[/b]
URI.toURL()
File.toURL()
ClassLoader.getSystemResource(String name)
Applet.getDocumentBase()



[b]URL有以下5部分组成[/b]
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

[b]URL编码解码[/b]
URLEncoder不会去判断这些字符如何用于URL,比如 = & 表示查询参数,因此必须将URL逐部分进行编码
URLDecoder不涉及非转义字符,所以可以传递整个URL,而不用将其分解。
public class URLEncoderExample {
public static void main(String[] args) throws UnsupportedEncodingException {
String input = "http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&stype=stext&q=+\"Java Network Programming\"";
System.out.println(input);
String output = URLEncoder.encode(input, "utf-8");
System.out.println(output);
String s = URLEncoder.encode("http", "utf-8");
s += "://";
s += URLEncoder.encode("www.altavista.com", "utf-8");
s += "/";
s += URLEncoder.encode("cgi-bin", "utf-8");
s += "/";
s += URLEncoder.encode("query", "utf-8");
s += "?";
s += URLEncoder.encode("pg", "utf-8");
s += "=";
s += URLEncoder.encode("q", "utf-8");
s += "&";
s += URLEncoder.encode("kl", "utf-8");
s += "=";
s += URLEncoder.encode("XX", "utf-8");
s += "&";
s += URLEncoder.encode("stype", "utf-8");
s += "=";
s += URLEncoder.encode("stext", "utf-8");
s += "&";
s += URLEncoder.encode("q", "utf-8");
s += "=";
s += URLEncoder.encode("\"Java Network Programming\"", "utf-8");
System.out.println(s);
}
}



[b]URL和URI[/b]
URL对象是从网络获取的应用程序协议的表示,而URI对象纯粹是可以解析和操作的字符串。
URI没有网络获取功能,URL有字符串解析方法getFile() getRef(),处理URL返回的字符串,分解为各个部分。
// URLSplitter
try {
URL u = new URL("http://xace:pwd@www.ibiblio.org/javafaq/books/jnp/index.html?isbn=12345#toc");
System.out.println("The URL is " + u);
System.out.println("The scheme/protocol is " + u.getProtocol());
System.out.println("The user info is " + u.getUserInfo());
String host = u.getHost();
if (host != null) {
int atSign = host.indexOf('@');
if (atSign != -1)
host = host.substring(atSign + 1);
System.out.println("The host is " + host);
} else {
System.out.println("The host is null.");
}
System.out.println("The port is " + u.getPort());
System.out.println("The defaultPort is " + u.getDefaultPort());
System.out.println("The path is " + u.getPath());
System.out.println("The file is " + u.getFile());
System.out.println("The ref is " + u.getRef());
System.out.println("The query string is " + u.getQuery());
System.out.println("The authority is:" + u.getAuthority());
} catch (MalformedURLException ex) {
System.err.println("is not a URL I understand.");
}

// URISplitter
try {
URI u = new URI("http://xace:pwd@www.ibiblio.org/javafaq/books/jnp/index.html?isbn=12345#toc");
System.out.println("The URI is " + u);
if (u.isOpaque()) {
System.out.println("This is an opaque URI.");
System.out.println("The scheme is " + u.getScheme());
System.out.println("The scheme specific part is " + u.getSchemeSpecificPart());
System.out.println("The fragment ID is " + u.getFragment());
} else {
System.out.println("This is a hierarchical URI.");
System.out.println("The scheme is " + u.getScheme());
try {
u = u.parseServerAuthority();
System.out.println("The host is " + u.getUserInfo());
System.out.println("The user info is " + u.getUserInfo());
System.out.println("The port is " + u.getPort());
} catch (URISyntaxException ex) {
System.out.println("The authority is " + u.getAuthority());
}
System.out.println("The path is " + u.getPath());
System.out.println("The query string is " + u.getQuery());
System.out.println("The fragment ID is " + u.getFragment());
}
} catch (URISyntaxException ex) {
System.err.println(" does not seem to be a URI.");
}


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

[b]代理[/b]
设置HTTP代理
// java.oreilly.com和xml.oreilly.com,不使用代理
System.setProperty("http.proxyHost", "192.168.5.1");
System.setProperty("http.proxyPort", "9000");
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类

[b]cookie[/b]
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值