java network programming 读书笔记(2) URI and URL

URI refers to Uniform Resource Identifier , 它的一般格式如下:

scheme:scheme-specific-part:fragment

 

如果scheme-specific-part是我们常用的格式,那它又可表示为:

scheme://authority/part?query

 

其中 scheme 包括 data、file、ftp, http 等,在android 中还有 content://

关于 authority 指什么呢? 能不能由用户自己定义呢?

authority可以指服务器,也可以指一个类(android中便是如此),它的责任是用来决定URI中authority后面的部分如何处理,英语原文是:

The authority part of the URI names the authority responsible for resolvin g the rest of the URI。

 

举个例子:

对于 http://www.ccniit.com/about/index.asp 来说,scheme是http, authority部分指 www.ccniit.com 它指的是一个具体的主机,当你向这个主机发送 /about/index.asp 查询时,这个authority(也就是这个主机)有责任决定如何对这一动作作出反应,在这里的反应就是把/about/index.asp 这个html文件发送到客户端的浏览器,但是这个authority有权改变这个动作,它可以自己定义自己想做出的行为。

 

下面是一个android中的例子。

我们常常使用 getContentResovler().query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) 这个方法,其时也就是通过 URI 来定位资源。 我们来分析一下这个方法。这个方法中的第一个参数就是URI,那么这个URI指的是什么呢,它不是指“主机”。每个ContentProvider都是一个类,也可以说是指一个对象,这个类会自己定义自己的URI,然后把这个URI在AndroidManifest.xml 文件中注册,这样整个程序都知道这个URI了。 当我们用这个URI时其实就是在调用我们自己定义的ContentProvider类,这个URI有一些规定,scheme必须是 content, 其它的自己指定,比如 content://com.facaizhu.net/contacts/2 我们在 AndroidManifest.xml 文件中注册 ContentProvider时同时注册了 scheme和authority,所以当程序遇到 content://com.facaizhu.net时它会自动找到处理的类,即我们自己编写的 ContentProvider,接下来的 /contacts/2 如何处理就是由我们自己来定义了。比如返回第二个人的联系信息。 Android系统本身也为我们提供了好多ContentProvider,我们都可以通过URI来访问它们,如 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI 是系统本身定义的一个常量,它的类型是Uri,我们可以直接访问这个URI。

 

如果scheme不是常用的的,则 scheme-specific-part可以由scheme决定,例如:tel:15936091458

 

URL 是URI的一程特殊情况,它主要用来访问网络资源,定义描述如下:

A URL identifies the location of a resource on the Internet. It specifies the protocol used to access a server (e.g., FTP, HTTP), the name of the server, and the location of a file on that server.

可以看到URL是URI的一个子集。

 

那么URI可以用什么字符呢,中文行吗? 答案请看下面一段话:

The scheme part is composed of lowercase letters, digits, and the plus sign, period, and hyphen. The other three parts of a typical URI (authority, path, and query) should each be composed of the ASCII alphanumeric characters; that is, the letters A-Z, a-z, and the digits 0-9. In addition, the punctuation characters - _ . ! ~ * ' may also be used. All other characters, including non-ASCII alphanumerics such as á and , should be escaped by a percent sign (%) followed by the hexadecimal code for the character. For instance, á would be encoded as %E1. A URL so transformed is said to have been "x-www-form-urlencoded".

 

也就是说: authority, path, query 都必须用 ascii 码! 否则就要进行编码,编成形如: %E1 形式。non-ASCII characters should be encoded by first converting them to UTF-8, then percent-escaping each byte of the UTF-8。 非ASCII码首先被转化为 UTF8编码,然后在每个字节后面加上%.

 

如果是不被允许的标点符号也是要编码的,即

Punctuation characters such as / and @ must also be encoded with percent escapes if they are used in any role other than what's specified for them in the scheme-specific part of a particular URL. For example, the forward slashes in the URI http://www.cafeaulait.org/books/javaio/ do not need to be encoded as %2F because they serve to delimit the hierarchy as specified for the http URI scheme. However, if a filename includes a / character—for instance, if the last directory were named Java I/O instead of javaio to more closely match the name of the book—the URI would have to be written as http://www.cafeaulait.org/books/Java%20I%2FO/. This is not as farfetched as it might sound to Unix or Windows users. Mac filenames frequently include a forward slash. Filenames on many platforms often contain characters that need to be encoded, including @, $, +, =, and many more.

 

一个URL的具体格式为:

protocol://username@hostname:port/path/filename?query#fragment

其中 protocol 可以包括:file, ftp, http, https, gopher, news, telnet, wais

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值