欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java网络编程URL和URI 博客分类: Java 编程网络应用Java网络协议Scheme 

程序员文章站 2024-02-21 20:02:46
...
获得URL的方法
URI.toURL()
File.toURL()
ClassLoader.getSystemResource(String name)
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,而不用将其分解。
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);
  }
}



URL和URI
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.");
}


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

代理
设置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类

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