URL结构
URL结构如下:
scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
其中:
- scheme: 描述访问服务器的协议,也就是如何获取该资源,最常见的是http,此外还有ftp等。
- user:password: 这里用于http认证
- host: 那里获取资源
- port: 连接端口号是多少
- path: 具体访问该资源的path
- query: 其他额外参数
- fragment: 片段,为client使用。还有一个params不太常用。
URL是只支持ascii字符表示的,其他的字符要经过utf-8编码操作。具体转义字符是 % ,比如空格SPACE,具体在URL中的表示就是 %20 。即使是ascii里面,也有一些字符需要转义,然后比如中文字符等,也需要如此utf-8编码和转义。比如 "书"
>>> '书'.encode('utf-8')
b'\xe4\xb9\xa6'
在URL中对应的就是 %e4%b9%a6 。还有其他一些细节,当然实际操作中各个编程语言都提供了良好的函数接口了的,比如python的urlencode函数。
http header详解
本小节主要参考了 这个网页 ,内容整理得不错,都搬过来了,做备份用。
http协议过程分为http请求过程和http响应过程,http请求过程就是发送http请求信息包,http响应过程就是发送http响应信息包的过程。
http 请求信息包
http请求信息包格式如下:
<method> <URL> <version>
<headers>
<entity-body>
比如打开google主页的请求包是:
GET / HTTP/1.1
Host: www.google.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:40.0) Gecko/20100101 Firefox/40.0
........
其中第一行为大家熟知, 就是 GET 方法,具体打开的URL是 / ,然后http的version是 HTTP/1.1 。然后后面的请求包header内容不定,后面再细讲。请求信息包大多都没有entity-body,也就是额外的content内容,但就算没有header完了也要留一空白行。
请求headers详解
| header | 解释 | 示例 |
|---|---|---|
| Accept | 指定客户端能够接收的内容类型 | Accept: text/plain, text/html |
| Accept-Charset | 浏览器可以接受的字符编码集 | Accept-Charset: iso-8859-5 |
| Accept-Encoding | 指定浏览器可以支持的web服务器返回内容压缩编码类型 | Accept-Encoding: compress, gzip |
| Accept-Language | 浏览器可接受的语言 | Accept-Language: en,zh |
| Accept-Ranges | 可以请求网页实体的一个或者多个子范围字段 | Accept-Ranges: bytes |
| Authorization | HTTP授权的授权证书 | Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== |
| Cache-Control | 指定请求和响应遵循的缓存机制 | Cache-Control: no-cache |
| Connection | 表示是否需要持久连接 (HTTP 1.1默认进行持久连接) | Connection: close |
| Cookie | HTTP请求发送时,会把保存在该请求域名下的所有cookie值一起发送给web服务器。 | Cookie: $Version=1; Skin=new; |
| Content-Length | 请求的内容长度 | Content-Length: 348 |
| Content-Type | 请求的与实体对应的MIME信息 | Content-Type: application/x-www-form-urlencoded |
| Date | 请求发送的日期和时间 | Date: Tue, 15 Nov 2010 08:12:31 GMT |
| Expect | 请求的特定的服务器行为 | Expect: 100-continue |
| From | 发出请求的用户的Email | From: user@email.com |
| Host | 指定请求的服务器的域名和端口号 | Host: www.zcmhi.com |
| If-Match | 只有请求内容与实体相匹配才有效 | If-Match: “737060cd8c284d8af7ad3082f209582d” |
| If-Modified-Since | 如果请求的部分在指定时间之后被修改则请求成功,未被修改则返回304代码 | If-Modified-Since: Sat, 29 Oct 2010 19:43:31 GMT |
| If-None-Match | 如果内容未改变返回304代码,参数为服务器先前发送的Etag,与服务器回应的Etag比较判断是否改变 | If-None-Match: “737060cd8c284d8af7ad3082f209582d” |
| If-Range | 如果实体未改变,服务器发送客户端丢失的部分,否则发送整个实体。参数也为Etag | If-Range: “737060cd8c284d8af7ad3082f209582d” |
| If-Unmodified-Since | 只在实体在指定时间之后未被修改才请求成功 | If-Unmodified-Since: Sat, 29 Oct 2010 19:43:31 GMT |
| Max-Forwards | 限制信息通过代理和网关传送的时间 | Max-Forwards: 10 |
| Pragma | 用来包含实现特定的指令 | Pragma: no-cache |
| Proxy-Authorization | 连接到代理的授权证书 | Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== |
| Range | 只请求实体的一部分,指定范围 | Range: bytes=500-999 |
| Referer | 先前网页的地址,当前请求网页紧随其后,即来路 | Referer: |
| TE | 客户端愿意接受的传输编码,并通知服务器接受接受尾加头信息 | TE: trailers,deflate;q=0.5 |
| Upgrade | 向服务器指定某种传输协议以便服务器进行转换(如果支持) | Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11 |
| User-Agent | User-Agent的内容包含发出请求的用户信息 | User-Agent: Mozilla/5.0 (Linux; X11) |
| Via | 通知中间网关或代理服务器地址,通信协议 | Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1) |
| Warning | 关于消息实体的警告信息 | Warn: 199 Miscellaneous warning |
http 响应信息包
http响应信息包格式如下:
<version> <status-code> <reason-phrase>
<headers>
<entity-body>
比如:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 231
Content-Type: text/html; charset=UTF-8
Date: Wed, 02 Sep 2015 08:47:52 GMT
....
这个响应体 HTTP/1.1 就是的就是http的version,然后 200 是具体的http状态码,然后 OK 是一个描述文字。然后是响应体的header,然后空一行,然后是响应信息包的具体发送的cotent内容。
http的方法method和状态码为大家所熟知,下面就header的一些内容列出来说明之。
响应headers详解
| header | 解释 | 示例 |
|---|---|---|
| Accept-Ranges | 表明服务器是否支持指定范围请求及哪种类型的分段请求 | Accept-Ranges: bytes |
| Age | 从原始服务器到代理缓存形成的估算时间(以秒计,非负) | Age: 12 |
| Allow | 对某网络资源的有效的请求行为,不允许则返回405 | Allow: GET, HEAD |
| Cache-Control | 告诉所有的缓存机制是否可以缓存及哪种类型 | Cache-Control: no-cache |
| Content-Encoding | web服务器支持的返回内容压缩编码类型。 | Content-Encoding: gzip |
| Content-Language | 响应体的语言 | Content-Language: en,zh |
| Content-Length | 响应体的长度 | Content-Length: 348 |
| Content-Location | 请求资源可替代的备用的另一地址 | Content-Location: /index.htm |
| Content-MD5 | 返回资源的MD5校验值 | Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ== |
| Content-Range | 在整个返回体中本部分的字节位置 | Content-Range: bytes 21010-47021/47022 |
| Content-Type | 返回内容的MIME类型 | Content-Type: text/html; charset=utf-8 |
| Date | 原始服务器消息发出的时间 | Date: Tue, 15 Nov 2010 08:12:31 GMT |
| ETag | 请求变量的实体标签的当前值 | ETag: “737060cd8c284d8af7ad3082f209582d” |
| Expires | 响应过期的日期和时间 | Expires: Thu, 01 Dec 2010 16:00:00 GMT |
| Last-Modified | 请求资源的最后修改时间 | Last-Modified: Tue, 15 Nov 2010 12:45:26 GMT |
| Location | 用来重定向接收方到非请求URL的位置来完成请求或标识新的资源 | Location: |
| Pragma | 包括实现特定的指令,它可应用到响应链上的任何接收方 | Pragma: no-cache |
| Proxy-Authenticate | 它指出认证方案和可应用到代理的该URL上的参数 | Proxy-Authenticate: Basic |
| refresh | 应用于重定向或一个新的资源被创造,在5秒之后重定向 | |
| Retry-After | 如果实体暂时不可取,通知客户端在指定时间之后再次尝试 | Retry-After: 120 |
| Server | web服务器软件名称 | Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) |
| Set-Cookie | 设置Http Cookie | |
| Trailer | 指出头域在分块传输编码的尾部存在 | Trailer: Max-Forwards |
| Transfer-Encoding | 文件传输编码 | |
| Vary | 告诉下游代理是使用缓存响应还是从原始服务器请求 | |
| Via | 告知代理客户端响应是通过哪里发送的 | |
| Warning | 警告实体可能存在的问题 | |
| WWW-Authenticate | 表明客户端请求实体应该使用的授权方案 | WWW-Authenticate: Basic |
http状态码
- MIME type: 其全称是Multipurpose Internet Mail Extensions ,可以看得出来和email系统有关,但不管怎么说,我们知道其在http里面用于描述文件类型即可。具体就是对应的
Content-type : image/jpeg
这一行,image/jpeg 就是MIME type描述,image是主文件类型,jpeg是次文件类型。最常见的是html文件,其MIME type是 text/html 。
- URI: Uniform Resource Identifier ,此外常见的还有URN和URL概念。参考了 这个网页 。URN和URL都属于URI的范畴,也就是都视同用一连串的字符串来将所有的资源文件分别表示出来,而URN和URL是两种不同的标识方法,URL有点类似于门牌号码街道的描述,其大致有如下结构:
scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
而URN比如:
urn:isbn:0451450523
目前几乎绝大部分URI就是URL,URN只在某些特别领域使用。
GET和POST的区别
参考了 这个网页 。前面谈及的修改网页的url来获取资源,实质就是HTTP的GET方法,也就是GET方法的信息就放在url上的,然后web service服务器会分析这些url,从而相应的决定对客户机的回应方式。而POST方法并不修改url,web service服务器接受的url上并没有任何额外的信息,具体POST方法具体会另外传输一个信息包。一般能够通过GET方法和服务器互动的就采用GET方法,但因为url的局限性,可能某些GET方法并不适用,这是就需要服务器支持对应的POST方法来互动了。至于PUT还有DELETE方法就更加少用了,有些服务器甚至根本就不支持这些冷门的方法。
HTTP返回错误码含义
100: ('Continue', 'Request received, please continue'),
101: ('Switching Protocols',
'Switching to new protocol; obey Upgrade header'),
200: ('OK', 'Request fulfilled, document follows'),
201: ('Created', 'Document created, URL follows'),
202: ('Accepted',
'Request accepted, processing continues off-line'),
203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
204: ('No Content', 'Request fulfilled, nothing follows'),
205: ('Reset Content', 'Clear input form for further input.'),
206: ('Partial Content', 'Partial content follows.'),
300: ('Multiple Choices',
'Object has several resources -- see URI list'),
301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
302: ('Found', 'Object moved temporarily -- see URI list'),
303: ('See Other', 'Object moved -- see Method and URL list'),
304: ('Not Modified',
'Document has not changed since given time'),
305: ('Use Proxy',
'You must use proxy specified in Location to access this '
'resource.'),
307: ('Temporary Redirect',
'Object moved temporarily -- see URI list'),
400: ('Bad Request',
'Bad request syntax or unsupported method'),
401: ('Unauthorized',
'No permission -- see authorization schemes'),
402: ('Payment Required',
'No payment -- see charging schemes'),
403: ('Forbidden',
'Request forbidden -- authorization will not help'),
404: ('Not Found', 'Nothing matches the given URI'),
405: ('Method Not Allowed',
'Specified method is invalid for this server.'),
406: ('Not Acceptable', 'URI not available in preferred format.'),
407: ('Proxy Authentication Required', 'You must authenticate with '
'this proxy before proceeding.'),
408: ('Request Timeout', 'Request timed out; try again later.'),
409: ('Conflict', 'Request conflict.'),
410: ('Gone',
'URI no longer exists and has been permanently removed.'),
411: ('Length Required', 'Client must specify Content-Length.'),
412: ('Precondition Failed', 'Precondition in headers is false.'),
413: ('Request Entity Too Large', 'Entity is too large.'),
414: ('Request-URI Too Long', 'URI is too long.'),
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
416: ('Requested Range Not Satisfiable',
'Cannot satisfy request range.'),
417: ('Expectation Failed',
'Expect condition could not be satisfied.'),
500: ('Internal Server Error', 'Server got itself in trouble'),
501: ('Not Implemented',
'Server does not support this operation'),
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
503: ('Service Unavailable',
'The server cannot process the request due to a high load'),
504: ('Gateway Timeout',
'The gateway server did not receive a timely response'),
505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),