java url构成及get属性方法解释

java 的url类中有很多get方法

以下是获取值的意义

// 首先先看一下wikipedia上关于url的一个描述

//Every HTTP URL conforms to the syntax of a generic URI. A generic URI is of the form:

 scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]

一个完整的 url 是由上面这些字段组成的。一般情况下我们访问的网络没有用户名密码验证,所以都没有输user:password

以下是在开发环境中对一个完整的url的输出的测试

String urlStr = "http://tester:123456@www.baidu.com?a=b&b=c&c=d#abc";

URL url = new URL(urlStr);

String protocol = url.getProtocol();
String host = url.getHost();
int port = url.getPort();
int defaultPort = url.getDefaultPort();
String query = url.getQuery();
String ref = url.getRef();
String user = url.getUserInfo();
String authority = url.getAuthority();
String file = url.getFile();
Object content = url.getContent();

System.out.printf("输出以上得到的结果");

protocol is http
host is www.baidu.com
port is -1
default port is 80 query is a=b&b=c&c=d
ref is abc
user is tester:123456
authority is tester:123456@www.baidu.com
file is ?a=b&b=c&c=d

content is sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1f89ab83

 



posted on 2016-03-25 17:14  码农时刻  阅读(2416)  评论(0编辑  收藏  举报