【README】
本文阐述了 URL, URI,以及对应的java类的api;
- 1.URI,统一资源标识符,标识互联网上的某个网络资源,标识方式如 名称,位置等;就像人的标识一样,可以通过身份证或学生证号或社保号码等来标识;(但不一定靠位置来标识)
- 2.URL, 统一资源定位符,唯一标识一个资源在internet上的位置(仅靠位置来标识);所以 URL 是 URI标识网络资源的一种方式;即 URI可标识的资源范围比URL大;
- 3.URL是一种URI ;
- 4. URI 由 URL + URN(统一资源名称)组成 ;
【1】URI,统一资源标识符
1)URI采用特定语法标识网络资源,URI 是一个标识资源的字符串而已;
2)URI标识语法 模式:模式特定部分;
2.1)模式包括(可以理解为资源协议):
- data
- file
- ftp
- http;
- telnet;
2.2)模式特定部分采用一种层次结构,如 //authority/path?k1=v1&k2=v2#anchor ;
- 其中 authority 表示授权机构,如 www.baidu.com 即服务器域名;
- path 表示资源路径;
- ? k1=v1&k2=v2 表示查询字符串;
- #anchor 锚点,表示html页面的某个元素;
【2】URL,统一资源定位符
1)URL 是一种URI, 除了标识一个网络资源,还提供资源的网络位置,以便查找; 客户端可以用它获取资源;
2)资源的网络位置包括 协议如http,服务器主机域名,端口,路径(查询参数,锚点或有); 3)URL语法如下:协议://userInfo@host:port/path?query#fragment
- 用户信息, 主机host,port端口合并在一起构成了权威机构;
- 路径是指向服务器上的一个特定目录或文件,类似于unix的文件系统路径;
- 查询字符串,向服务器提供了附加参数;
- 片段, 指向远程资源的某个特定部分;
【3】URL java类例子
【3.1】创建URL
@Testpublic void f1() throws MalformedURLException {URL url = new URL("http://www.baidu.com");System.out.println(url);System.out.println(url.getProtocol());System.out.println(url.getPort());System.out.println(url.getAuthority());}
http://www.baidu.com
http
-1
www.baidu.com
【3.2】创建URL-相对地址
@Testpublic void f2() throws IOException {URL url1 = new URL("https://blog.csdn.net/PacosonSWJTU/article/details/120964766");URL url2 = new URL(url1, "120980127"); // 相对地址System.out.println(url1.getPath()); // /PacosonSWJTU/article/details/120964766System.out.println(url2.getPath()); // /PacosonSWJTU/article/details/120980127}
从url获取数据(3种方式)
- 方法1,使用 URL.openStream() ;
- 方法2,使用 URL.openConnection 和 URLCOnnection.getInputStream() 方法;
- 方法3,使用 URL.getContent() 方法;
补充,URL.openStream() 方法如下:
public final InputStream openStream() throws java.io.IOException {return openConnection().getInputStream(); // 获取输入流 }// 打开连接
public URLConnection openConnection() throws java.io.IOException {return handler.openConnection(this);}
3.1)方法1,使用openStream() 方法
@Testpublic void f3() throws IOException {URL url1 = new URL("https://blog.csdn.net/PacosonSWJTU/article/details/120964766");try (DataInputStream dataInputStream = new DataInputStream(url1.openStream())) {int c;while((c = dataInputStream.read()) != -1) {System.out.print((char)c); // 有乱码 无法对中文进行解析}}}
改用基于UTF-8字符编码格式的 inputStreadReader 读取数据,(可以处理中文)如下:
@Testpublic void f4() throws IOException {URL url1 = new URL("https://blog.csdn.net/PacosonSWJTU/article/details/120964766");// 采用utf-8 编码,可以解析中文字符String str = null;try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url1.openStream(), StandardCharsets.UTF_8))) {while((str = bufferedReader.readLine()) != null) {System.out.print(str);}}}
方法2,使用 URL.openConnection() 和 URLCOnnection.getInputStream() 方法 读取资源;
/*** @title 从URL获取资源内容-url1.openConnection()+urlConnection.getInputStream()方法* @author xiaotang* @updateTime 2021/10/31*/@Testpublic void f4_2() throws IOException {URL url1 = new URL("https://blog.csdn.net/PacosonSWJTU/article/details/120964766");// 采用utf-8 编码,可以解析中文字符String str = null;URLConnection urlConnection = url1.openConnection();try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), StandardCharsets.UTF_8))) {while((str = bufferedReader.readLine()) != null) {System.out.print(str);}}}
方法3,使用 URL.getContent() 方法读取资源
/*** 通过 getContent() 获取资源内容*/@Testpublic void f5() throws IOException {URL url1 = new URL("https://blog.csdn.net/PacosonSWJTU/article/details/120964766");// getContent() 获取资源内容Object o = url1.getContent();System.out.println(o); // sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@694e1548}/*** 通过 getContent(Class[] classes) 获取资源内容*/@Testpublic void f6() throws IOException {URL url1 = new URL("https://blog.csdn.net/PacosonSWJTU/article/details/120964766");Class<?>[] arr = {String.class, Reader.class, InputStream.class};Object o = url1.getContent(arr);System.out.println(o); // sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@694e1548InputStream inputStream = (InputStream) o ;BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));String result = null;while((result = bufferedReader.readLine()) != null) {System.out.println(result);}}
【3.3】分解URL
1)URL 包括5部分;
- 模式,也称协议;
- 授权机构;
- 路径;
- 片段标识符,或段或ref;
- 查询字符串;
/*** @title 分解URL, URL 包括5部分* @author xiaotang* @updateTime 2021/10/31*/@Testpublic void f6() throws IOException {URL url = new URL("https://blog.csdn.net/PacosonSWJTU/article/details/120904564?k1=v1&k2=v2#anchor=1");System.out.println(url.getProtocol());System.out.println(url.getHost());System.out.println(url.getPort());System.out.println(url.getFile());System.out.println(url.getPath());System.out.println(url.getRef());System.out.println(url.getQuery());System.out.println(url.getUserInfo());System.out.println(url.getAuthority()); }
https
blog.csdn.net
-1
/PacosonSWJTU/article/details/120904564?k1=v1&k2=v2
/PacosonSWJTU/article/details/120904564
anchor=1
k1=v1&k2=v2
null
blog.csdn.net
2)URL相等性
/*** @title URL 相等性* @author xiaotang* @updateTime 2021/10/31*/@Testpublic void f7() throws IOException {URL url1 = new URL("https://blog.csdn.net/PacosonSWJTU/article/details/120904564?k1=v1&k2=v2#anchor=1");URL url2 = new URL("https://blog.csdn.net/PacosonSWJTU/article/details/120904564?k1=v1&k2=v2#anchor=1");if (url1.equals(url2)) { // equals 底层调用了 sameFile() 方法System.out.println(url1 + "is equals to " + url2);} else {System.out.println(url1 + "is not equals to " + url2);}System.out.println(url1.sameFile(url2)); // true }
3)URL 比较
// URL比较,调用URl.toString() toExternalForm(), toURI()@Testpublic void f8() throws Exception {URL url1 = new URL("http://www.baidu.com?name=张三&city=成都#anchor=2");System.out.println(url1.toString()); //System.out.println(url1.toExternalForm());URI uri2 = url1.toURI(); // URL 转为 URI 类System.out.println(uri2);}
【4】URI类-统一资源标识符类
1)URI是 URL的抽象,不仅包括统一资源定位符URL,还包括统一资源名 URN;
2)URI 提供了网络资源的表示,但没有提供网络获取功能;而URL 提供了网络资源获取功能;
3)URI VS URL (非常重要)
- 若想下载一个 URL的内容,应该使用URL;
- 如果想用URL 来标识而不是获取网络资源(如一个 XML的命名空间),就应该使用 URI类;
补充:URI.toURL() 可以把 URI 转为 URL; URL.toURI() 可以把 URL 转为 URI;
简单说,URI是抽象的定义,不管用什么方法表示,只要能定位一个资源,就叫URI;
本来设想的的使用两种方法定位:1,URL,用地址定位;2,URN 用名称定位。
举个例子:去村子找个具体的人(URI),如果用地址:某村多少号房子第几间房的主人 就是URL, 如果用身份证号+名字 去找就是URN了。
结果就是 目前WEB上就URL流行开了,平常见得URI 基本都是URL。
4) URI vs URL 例子 (干货)
// URI vs URL URI 标识资源,URL 定位资源但可能无法标识某些资源@Testpublic void f14() throws Exception {URI voice = new URI("tel:+1-800-9988-9938"); // 电话URI book = new URI("urn:isbn:1-023-40287-9"); // 国际标准书号URI email = new URI("zhangsan@goole.com"); // 邮件// 打印 URISystem.out.println("==== 打印 URI");System.out.println(voice);System.out.println(book);System.out.println(email);// 打印 URLSystem.out.println("==== 打印 URL");System.out.println(new URL("http://www.baidu.com")); // http://www.baidu.com, 必须以协议开头 System.out.println(new URL("tel:+1-800-9988-9938")); // 会抛出异常哦System.out.println(new URL("urn:isbn:1-023-40287-9")); // 会抛出异常哦System.out.println(new URL("zhangsan@goole.com")); // 会抛出异常哦}
打印结果:
==== 打印 URI
tel:+1-800-9988-9938
urn:isbn:1-023-40287-9
zhangsan@goole.com
==== 打印 URL
http://www.baidu.comjava.net.MalformedURLException: unknown protocol: tel // 抛出异常了
【4.1】构造URI
1)构建URI;
// 相对uri@Testpublic void f11() throws Exception {URI uri = new URI("https://blog.csdn.net/PacosonSWJTU/article/details/120964766");System.out.println(uri);URI uri2 = uri.resolve("59483747"); // 相对地址 59483747System.out.println(uri2); // https://blog.csdn.net/PacosonSWJTU/article/details/59483747}
2)URI的各个部分
模式:模式特定部分:片段;
// 获取URI属性@Testpublic void f10() throws Exception {URI uri = new URI("https://blog.csdn.net/PacosonSWJTU/article/details/120904564?k1=v1&k2=张三#anchor=1");System.out.println(uri.isOpaque());System.out.println(uri.getAuthority());System.out.println(uri.getFragment());System.out.println(uri.getHost());System.out.println(uri.getPath());System.out.println(uri.getPort());System.out.println(uri.getQuery());System.out.println(uri.getUserInfo());/* 获取原生数据 */System.out.println("----------------- raw -------------------");System.out.println(uri.getRawAuthority());System.out.println(uri.getRawFragment());System.out.println(uri.getRawPath());System.out.println(uri.getRawQuery());System.out.println(uri.getRawUserInfo());System.out.println(uri.getRawSchemeSpecificPart());}
false
// 如果 URI是一个层次URI,则返回false; 若URI不是层次URI,即不是透明的,返回true
blog.csdn.net
anchor=1
blog.csdn.net
/PacosonSWJTU/article/details/120904564
-1
k1=v1&k2=张三
null
----------------- raw -------------------
blog.csdn.net
anchor=1
/PacosonSWJTU/article/details/120904564
k1=v1&k2=张三
null
//blog.csdn.net/PacosonSWJTU/article/details/120904564?k1=v1&k2=张三
【4.2】相对uri
@Testpublic void f11() throws Exception {URI uri = new URI("https://blog.csdn.net/PacosonSWJTU/article/details/120964766");System.out.println(uri);URI uri2 = uri.resolve("59483747"); // 相对地址 59483747System.out.println(uri2); // https://blog.csdn.net/PacosonSWJTU/article/details/59483747}
【4.3】uri 字符串表示
toASCIIString 对中文字符采用了 x-www-form-urlencode格式 编码;
// uri 字符串表示@Testpublic void f12() throws Exception {URI uri = new URI("https://blog.csdn.net/PacosonSWJTU/article/details/120964766?name=张三");System.out.println(uri.toString()); // https://blog.csdn.net/PacosonSWJTU/article/details/120964766?name=张三 System.out.println(uri.toASCIIString()); // https://blog.csdn.net/PacosonSWJTU/article/details/120964766?name=%E5%BC%A0%E4%B8%89}
【4.4】uri 编码与解码
1) x-www-form-urlencoded
1.1)介绍: intro2 x-www-form-urlencode, refer2
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Methods/POST
2)编码与解码
// 对url进行编码与解码 格式为 x-www-form-urlencode ;// intro2 x-www-form-urlencode, refer2 https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Methods/POST@Testpublic void f13() throws Exception {System.out.println(URLEncoder.encode("hello world", StandardCharsets.UTF_8.displayName()));System.out.println(URLEncoder.encode("hello world 张三", StandardCharsets.UTF_8.displayName())); // 编码String str = URLEncoder.encode("hello world 张三", StandardCharsets.UTF_8.displayName());// 编码-> hello+world+%E5%BC%A0%E4%B8%89String decodedStr = URLDecoder.decode(str, StandardCharsets.UTF_8.displayName());// 解码->hello world 张三System.out.println(decodedStr);}
3)url编码方式
3.1)背景
在发明web时,unicode 还没有完全普及,所以并不是所有系统都可以处理 本(中文)之类的字符;为了解决这个问题,URL 中使用的字符必须来自 ASCII的一个固定的子集,包括:
- 大写字母 A-Z‘;
- 小写字母: a-z ;
- 数组 0-9;
- 标点符号字符 - _ . ! ~ * ` ( , ) 等;
3.2)url编码方式如下:
除了 ascii数字,字母和前面指定的标点符号外,所有其他字符都要转换为字节,每个字节要写为 百分号后面加两个16进制数字;
空格是一种特殊情况, 因为它太普遍了;除了编码为 %20,还可以编码为 为加号+;加号本身编码为 %2B ;