• HTTP协议基础


    1、HTTP协议简介

    HTTP协议,全称Hyper Text Transfer Protocal,超文本传输协议,是互联网中最常用的一种网络协议。HTTP的重要应用之一是web服务,设计HTTP协议最初的目的就是提供一种发布和接收HTML页面的方法(请求和响应)。

    HTTP协议是互联网上常用的通信协议之一,他又很多的应用,但最主要的就是web浏览器和web服务器之间的通信,即WWW应用或者web应用。

    WWW,全称World Wide Web,常称为web,中文叫万维网。HTTP协议的WWW服务应用的默认端口为80(端口的概念),另外的一个加密的WWW服务应用https的默认端口为443,主要用于网银、支付宝等和钱有关的业务。

    HTTP协议的主要特点概括如下:支持客户端/服务器模式

    无连接,无连接的含义是限制每一次连接只处理一个请求,服务器处理完客户端请求,并收到客户端应答后,断开连接,这种方式可以节省时间。

    无状态:HTTP协议是无状态协议,无状态是指协议本身对事务的处理没有记忆能力,缺少状态意味着如果后续处理需要之前的信息,必须重传,导致每次传输数据量增大。人生若只如初见

    【状态】的含义就是:客户端和服务器在某次会话中产生的数据,从而【无状态】就意味着,这些数据不会被保留

    2、URL同一资源定位符

    URL的格式:  <scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>
    URI表示URL中的“/<path>;<params>?<query>#<frag>”这一部份。

    3、一次完整的http请求过程

    1、客户端在浏览器输入要访问的域名
    
    2、浏览器请求DNS解析,将域名解析成IP
    
    3、客户端浏览器从访问IP解析出端口
    
    4、建立TCP连接(三次握手)
    
    5、客户端浏览器向服务端发送请求
    
      客户端发送请求头信息:建立连接后,客户端发送一个请求给服务器,请求方式的格式为:统一资源标识符、协议版本号
    
    6、服务器应答:
    
      服务器接收到请求后,发送响应信息,格式为:状态行(包括信息的协议版本号、状态吗),后面是MIME信息包括服务器信息、实体信息和可能的内容
    
    7、服务器发送响应头信息
    
    8、服务器向浏览器发送数据(响应报文主体)
    
    9、服务器关闭TCP连接(四次断开)在7层发送关闭请求到TCP层

    HTTP请求报文和响应报文

    请求报文

    请求报文格式

    报文信息

    请求的起始行

    GET / HTTP/1.1

    请求头部

    空行

    无内容

    请求报文主体

    GET方法无请求报文主体,POST方法才有

    请求行之后是请求首部。首部常见的部分有如下几个:

    • Accept:请求的对象类型。如果是“/”表示任意类型,如果是指定的类型,则会变成“type/”。
    • Accept-Language:使用的语言种类。
    • Accept-Encording:页面编码种类。
    • Accept-Charset:页面字符集。
    • User-Agent:提供了客户端浏览器的类型和版本。
    • Host:连接的目标主机,如果连接的服务器是非标准端口,在这里会出现使用的非标准端口。
    • Connection:对于HTTP连接的处理,keep-alive表示保持连接,如果是在响应报文中发送页面完毕就会关闭连接,状态变为close。

    响应报文

    响应报文格式

    报文信息

    响应的起始行

    响应状态 HTTP/1.1 200 OK

    响应头

    Server: bfe/1.0.8.18

    Date: Tue, 04 Jul 2017 06:33:34 GMT

    Content-Type: text/html

    Content-Length: 2381

    Last-Modified: Mon, 23 Jan 2017 13:27:32 GMT

    Connection: Keep-Alive

    空行

    无内容

    响应报文的主体:(文件的内容)

    <!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 v

    响应头字段:

    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: http://www.zcmhi.com/archives/94.html
    Pragma 包括实现特定的指令,它可应用到响应链上的任何接收方 Pragma: no-cache
    Proxy-Authenticate 它指出认证方案和可应用到代理的该URL上的参数 Proxy-Authenticate: Basic
    refresh 应用于重定向或一个新的资源被创造,在5秒之后重定向(由网景提出,被大部分浏览器支持)
     
    Refresh: 5; url=
    http://www.zcmhi.com/archives/94.html
    Retry-After 如果实体暂时不可取,通知客户端在指定时间之后再次尝试 Retry-After: 120
    Server web服务器软件名称 Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
    Set-Cookie 设置Http Cookie Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1
    Trailer 指出头域在分块传输编码的尾部存在 Trailer: Max-Forwards
    Transfer-Encoding 文件传输编码 Transfer-Encoding:chunked
    Vary 告诉下游代理是使用缓存响应还是从原始服务器请求 Vary: *
    Via 告知代理客户端响应是通过哪里发送的 Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
    Warning 警告实体可能存在的问题 Warning: 199 Miscellaneous warning
    WWW-Authenticate 表明客户端请求实体应该使用的授权方案 WWW-Authenticate: Basic

    使用curl命令查看请求报文和响应报文

    查看请求报文

    [root@ans3 conf]# curl -I www.baidu.com
    HTTP/1.1 200 OK
    Accept-Ranges: bytes
    Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
    Connection: keep-alive
    Content-Length: 277
    Content-Type: text/html
    Date: Sun, 17 May 2020 15:15:13 GMT
    Etag: "575e1f5c-115"
    Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT
    Pragma: no-cache
    Server: bfe/1.0.8.18

    查看响应报文

    [root@ans3 conf]# curl -i  www.baidu.com
    HTTP/1.1 200 OK
    Accept-Ranges: bytes
    Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
    Connection: keep-alive
    Content-Length: 2381
    Content-Type: text/html
    Date: Sun, 17 May 2020 15:18:06 GMT
    Etag: "588604c4-94d"
    Last-Modified: Mon, 23 Jan 2017 13:27:32 GMT
    Pragma: no-cache
    Server: bfe/1.0.8.18
    Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
    
    <!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>

    使用curl命令查看http请求各阶段耗时

    准备模板文件

    [root@ans3 ~]# vim curl.txt
    
    
                time_namelookup:  %{time_namelookup}
    
                   time_connect:  %{time_connect}
    
                time_appconnect:  %{time_appconnect}
    
               time_pretransfer:  %{time_pretransfer}
    
                  time_redirect:  %{time_redirect}
    
             time_starttransfer:  %{time_starttransfer}
    
                                ----------
    
                     time_total:  %{time_total}
    
    
    

    测试

    [root@ans3 ~]# curl -w "@curl.txt" -o /dev/null -s https://www.sogo.com
    
                time_namelookup:  0.014
                   time_connect:  0.028
                time_appconnect:  0.518
               time_pretransfer:  0.518
                  time_redirect:  0.000
             time_starttransfer:  0.546
                                ----------
                     time_total:  0.546

    字段解释

    NAMELOOKUP:从开始计算,域名解析完成的耗时
    
    CURLINFO_NAMELOOKUP_TIME. The time it took from the start until the name resolving was completed.
    
    CONNECT:从开始计算,TCP建立完成的耗时
    
    CURLINFO_CONNECT_TIME. The time it took from the start until the connect to the remote host (or proxy) was completed.
    
    APPCONNECT:从开始计算,应用层(SSL,在TCP之上的应用层)连接/握手完成的耗时
    
    CURLINFO_APPCONNECT_TIME. The time it took from the start until the SSL connect/handshake with the remote host was completed. (Added in in 7.19.0)
    
    PRETRANSFER:从开始计算,准备开始传输数据的耗时
    
    CURLINFO_PRETRANSFER_TIME. The time it took from the start until the file transfer is just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.
    
    STARTTRANSFER:从开始计算,开始传输数据的耗时(libcurl接收到第一个字节)
    
    CURLINFO_STARTTRANSFER_TIME. The time it took from the start until the first byte is received by libcurl.
    
    TOTAL:总的耗时
    
    CURLINFO_TOTAL_TIME. Total time of the previous request.
    
    REDIRECT:整个过程重定向的耗时,如果整个过程没有重定向,这个时间为0

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    P85 实战练习
    Java语句及控制台输入
    P62 实战练习
    运算符、数据类型转换和注释
    Java变量与常量
    Java语法初步实际应用
    JQ 练习题
    JQ 特效下拉列表 写出与css一样的效果
    JQ 全选设定与设置选中
    JQ JSON数据类型
  • 原文地址:https://www.cnblogs.com/zh-dream/p/12907736.html
Copyright © 2020-2023  润新知