前端系统班 - 06. HTTP 入门

2018-05-07  本文已影响0人  诸葛战鹰

课程 01:

WWW 的历史

WWW 的发明

主要包含 3 个概念:

  1. URI(Uniform Resource Identifier,统一资源标识符);
  2. HTTP(HyperText Transfer Protocol,超级文本传输协议);
  3. HTML(HyperText Markup Language,超文本标记语言)。

URI 是什么

URI 分为:

通过 URL 可以确定一个“唯一的”地址(网址),例如:
https://www.baidu.com/s?wd=hello&rsv_spt=1#5
这就是一个 URL 地址,其中:https:// 表示协议;www.baidu.com 表示域名;/s 表示路径;wd=hello&rsv_spt=1 是查询参数;最后 #5 是锚点。

DNS 是什么

DNS(Domain Name System,域名系统),它的作用简单来讲就是:输入域名,输出 IP(Internet Protocol,互联网协议)。

在命令行中,使用 nslookup 命令可以查看域名对应的 IP 地址;使用 ping 命令可以知道域名对应的 IP 地址以及与该域名服务器的连接情况。

请求与响应

在这过程中,HTTP 指导浏览器和服务器如何进行沟通。

请求示例

在命令行中,使用 curl 命令可以访问一个域名。例如:
curl -s -v -H "xxx" -- "https://www.baidu.com"
响应数据如下:

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
* STATE: INIT => CONNECT handle 0x48ef160; line 1404 (connection #-5000)
* Rebuilt URL to: http://www.baidu.com/
* Added connection 0. The cache now contains 1 members
* STATE: CONNECT => WAITRESOLVE handle 0x48ef160; line 1440 (connection #0)
*   Trying 61.135.169.125...
* TCP_NODELAY set
* STATE: WAITRESOLVE => WAITCONNECT handle 0x48ef160; line 1521 (connection #0)
* Connected to www.baidu.com (61.135.169.125) port 80 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x48ef160; line 1573 (connection #0)
* Marked for [keep alive]: HTTP default
* STATE: SENDPROTOCONNECT => DO handle 0x48ef160; line 1591 (connection #0)
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.59.0
> Accept: */*
>
* STATE: DO => DO_DONE handle 0x48ef160; line 1670 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x48ef160; line 1795 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x48ef160; line 1811 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK
* Server bfe/1.0.8.18 is not blacklisted
< Server: bfe/1.0.8.18
< Date: Mon, 07 May 2018 08:02:59 GMT
< Content-Type: text/html
< Content-Length: 2381
< Last-Modified: Mon, 23 Jan 2017 13:27:32 GMT
< Connection: Keep-Alive
< ETag: "588604c4-94d"
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Pragma: no-cache
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< Accept-Ranges: bytes
<
{ [2381 bytes data]
* STATE: PERFORM => DONE handle 0x48ef160; line 1980 (connection #0)
* multi_done
* Connection #0 to host www.baidu.com left intact
* Expire cleared

其中红色大于号开头的行,便是请求的内容;而后面的小于号开头的行则是返回内容。

GET 与 POST

改用 POST 请求,curl -X POST -s -v -H "xxx" -- "https://www.baidu.com",返回“页面不存在”页面……

两者的区别可以从字面意思理解,GET 就是获取数据,POST 就是提交数据。

请求的格式

1 动词 路径 协议/版本
2 key1: value1 // 例如:Content-Type: application/x-www-form-urlencoded
  key2: value2 // 例如:Host: www.baidu.com 
  key3: value3 // 例如:User-Agent: curl/7.54.0
  ...
3
4 要上传的数据
  1. 请求最多包含四部分,最少包含三部分(第四部分可以为空)。
  2. 第三部分永远都是一个回车 \n,主要是用来区分第二部分和第四部分内容的。
  3. 动词有 GET(获取)、POST(新增或上传)、PUT(整体更新)、PATCH(局部更新)、DELETE(删除)、HEAD、OPTIONS等。
  4. 请求中的路径包括“查询参数”,但不包括“锚点”;因为“锚点”不是由服务器识别,而是由浏览器识别的。
  5. 如果没有写路径,那么路径默认为 /,代表根目录。
  6. 第二部分中的 Content-Type 标注了第四部分的格式。

响应

响应的格式

1 协议/版本 状态码 状态解释
2 key1: value1 // 例如:Content-Length: 17931
  key2: value2 // 例如:Content-Type: text/html
  ...
3
4 要下载的内容

如果要在 Chrome 里看请求或相应的代码时,记得点击 view source 选项……这老师念了 N 遍……

上一篇 下一篇

猜你喜欢

热点阅读