URL 和 NSURL 格式解析

URL 和 NSURL 格式解析

字数973阅读46评论0喜欢0

1. URL 是什么?

统一资源定位符(Uniform Resource Locator,缩写为URL),又叫做网页地址,是互联网上标准的资源的地址(Address)。互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它。

统一资源定位符:

定位资源

确定资源出自哪里(可以用什么方式解析)

2. URL 定义格式

注意,Windows 主机不区分 URL 大小写,但是,Unix/Linux 主机区分大小写。

url 的基本定义:

// url 编码格式foo://example.com:8042/over/there?name=ferret#nose  \_/ \______________/ \________/\_________/ \__/|        |              |        |        |scheme    authority        path    query  fragment

URL由三部分组成:资源类型、存放资源的主机域名、资源文件名

scheme://host.domain:port/path/filenamescheme - 定义因特网服务的类型。最常见的类型是 httphost - 定义域主机(http 的默认主机是 www)domain - 定义因特网域名,比如 w3school.com.cn:port - 定义主机上的端口号(http 的默认端口号是80)path - 定义服务器上的路径(如果省略,则文档必须位于网站的根目录中)。filename - 定义文档/资源的名称

常见的 scheme

file      资源是本地计算机上的文件。格式file://ftp        通过 FTP访问资源。格式 FTP://gopher    通过 Gopher 协议访问该资源。 http      通过 HTTP 访问该资源。 格式 HTTP://https      通过安全的 HTTPS 访问该资源。 格式 target=_blank>HTTPS://mailto    资源为电子邮件地址,通过 SMTP 访问。 格式 mailto:MMS        通过 支持MMS(流媒体)协议的播放该资源。(代表软件:Windows Media Player)格式 MMS://ed2k      通过 支持ed2k(专用下载链接)协议的P2P软件访问该资源。(代表软件:电驴) 格式 ed2k://Flashget  通过 支持Flashget:(专用下载链接)协议的P2P软件访问该资源。(代表软件:快车) 格式 Flashget://thunder    通过 支持thunder(专用下载链接)协议的P2P软件访问该资源。(代表软件:迅雷) 格式 thunder://news      通过 NNTP 访问该资源。

常见的URL中定位和标识的服务或文件:

http:        文件在WEB服务器上.

file:        文件在您自己的局部系统或匿名服务器上

ftp:        文件在FTP服务器上

gopher:      文件在gopher服务器上

wais:        文件在wais服务器上

news:        文件在Usenet服务器上

telnet:      连接到一个支持Telnet远程登录的服务器上

3. NSURL

NSURL 继承自 NSObject, NSURL 就是对统一资源定位符的数据封装。包含的也就是组成 url 的三部分信息。

NSURL相关属性

publicvarabsoluteString:String{get}    publicvarrelativeString:String? {get}// The relative portion of a URL.  If baseURL is nil, or if the receiver is itself absolute, this is the same as absoluteString@NSCopyingpublicvarbaseURL:NSURL? {get}// may be nil.@NSCopyingpublicvarabsoluteURL:NSURL{get}// if the receiver is itself absolute, this will return self./* Any URL is composed of these two basic pieces.  The full URL would be the concatenation of [myURL scheme], ':', [myURL resourceSpecifier]

*/publicvarscheme:String{get}    publicvarresourceSpecifier:String{get}/* If the URL conforms to rfc 1808 (the most common form of URL), the following accessors will return the various components; otherwise they return nil.  The litmus test for conformance is as recommended in RFC 1808 - whether the first two characters of resourceSpecifier is @"//".  In all cases, they return the component's value after resolving the receiver against its base URL.

*/publicvarhost:String? {get}@NSCopyingpublicvarport:NSNumber? {get}    publicvaruser:String? {get}    publicvarpassword:String? {get}    publicvarpath:String? {get}    publicvarfragment:String? {get}    publicvarparameterString:String? {get}    publicvarquery:String? {get}    publicvarrelativePath:String? {get}// The same as path if baseURL is nil/* Determines if a given URL string's path represents a directory (i.e. the path component in the URL string ends with a '/' character). This does not check the resource the URL refers to.

*/@available(iOS9.0, *)    publicvarhasDirectoryPath:Bool{get}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,962评论 19 139
  • 1. URL 是什么? 统一资源定位符(Uniform Resource Locator,缩写为URL),又叫做网...
    Laughingg阅读 3,454评论 0 0
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,211评论 30 472
  • 前些日子在小镇 看到一个戴着耳机漫游在田埂的连帽衫的少年 天空渐渐下起了大雨 他不打伞 走过一拱小桥 路过一条十字...
    优优和吖吖阅读 1,200评论 1 1