HTTP协议分享

  • 定义
  • HTTP协议组成
    • 请求报文 request
      • 请求结构如图
      • 请求方法
        • GET
        • POST
        • HEAD
        • PUT
        • DELETE
        • OPTIONS
        • TRACE
    • 响应报文 response
      • 响应结构
      • 响应码
        • 1xx
        • 2xx
          • 200 OK
          • 206 Partial Content
        • 3xx
          • 301 永久重定向
          • 302 临时重定向
          • 304 Not Modified
          • 307 Temporary Redirect
        • 4xx
          • 401 Unauthorized
          • 405 Method Not Allowed
          • 416 Requested Range Not Satisfiable
        • 5xx
  • 常用用法
    • 防盗链+数据统计 - Referer
      • 防盗链
      • 数据统计
    • 断点续传+多线程下载 - Range

督促自己学习总结,特用文章的形式记录下来,共同进步

定义

HTTP(HyperText Transfer Protocol)超文本传输协议(超文本转移协议)
HTTP是一种不保存状态,即无状态协议(状态保存引入Cookie)
HTTP是应用层协议
Web网页(如:购物网站、博客) 开发常用各种api,android中OkHttp框架

置灰后的效果

HTTP协议组成

请求报文 request

请求结构如图

POST /blog HTTP/1.1
Host: localhost:3434
User-Agent: curl/7.63.0
Accept: */*
Content-Length: 14
Content-Type: application/x-www-form-urlencodedname=zy&age=28简单一个请求报文请求行,请求头部和请求实体

请求方法

GET

telnet baidu.com 80
GET / HTTP/1.1
Host: www.baidu.comcurl -v www.baidu.com分别用telnet 和curl 发送GET请求

POST

POST /blog HTTP/1.1
Host: localhost:3434
Content-Length: 93
Content-Type: application/x-www-form-urlencoded{"id":1,"date":"2020-02-22 03:29:13","author":"fish","title":"http2","content":"http test 2"}curl -v -X POST  --data "{\"id\":2,\"date\":\"2020-02-22 03:29:13\",\"author\":\"fish\",\"title\":\"http2\",\"content\":\"http test 2\"}" http://localhost:3434/blog/1

HEAD

只返回响应头,不返回实体

telnet baidu.com 80
HEAD / HTTP/1.1
Host: www.baidu.com
Connection: Closecurl -v -X HEAD www.baidu.com
curl -v -I www.baidu.com
curl -v --head www.baidu.com

PUT

curl -v -X PUT --data "{\"id\":2,\"date\":\"2020-02-22 03:29:13\",\"author\":\"fish\",\"title\":\"http2\",\"content\":\"http test 2\"}" http://localhost:3434/blog/1
curl -v -X PUT  --data-ascii "{\"id\":2,\"date\":\"2020-02-22 03:29:13\",\"author\":\"fish\",\"title\":\"http2\",\"content\":\"http test 2\"}" http://localhost:3434

DELETE

curl -v -X DELETE http://localhost:3434/blog

OPTIONS

curl -X OPTIONS https://developer.mozilla.org/ -i
curl -X OPTIONS example.org -itelnet example.org 80
OPTIONS / HTTP/1.1
Host: example.org
86183@DESKTOP-MKB5ERF MINGW64 ~/Desktop
$ curl -X OPTIONS example.org -i% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0HTTP/1.1 200 OK
Allow: OPTIONS, GET, HEAD, POST
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Fri, 05 Jun 2020 00:39:33 GMT
Expires: Fri, 12 Jun 2020 00:39:33 GMT
Server: EOS (vny/0453)
Content-Length: 0支持4中方法:Allow: OPTIONS, GET, HEAD, POST

TRACE

curl -v -X TRACE www.baidu.com -H "Max-Forwards: 2"

还有CONNECT,PATCH等

响应报文 response

响应结构

HTTP/1.1 302 Found
Connection: keep-alive
Content-Security-Policy-Report-Only: default-src https: 'unsafe-inline' 'unsafe-eval' data: blob: ; report-uri https://reports.baidu.com/csp-report/baike
Content-Type: text/html; charset=UTF-8
Date: Fri, 05 Jun 2020 00:07:24 GMT
Location: //baike.baidu.com/item/curl
Server: Apache
Content-Length: 0

响应码

1xx

信息,服务器收到请求,需要请求者继续执行操作
范例待补充

2xx

请求成功

200 OK

请求成功

curl -v -I http://a4.att.hudong.com/21/09/01200000026352136359091694357.jpg
 curl  -I http://a4.att.hudong.com/21/09/01200000026352136359091694357.jpg% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed0  247k    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0HTTP/1.1 200 OK
Date: Fri, 05 Jun 2020 00:45:26 GMT
Content-Type: image/jpg
Content-Length: 253354
Connection: keep-alive
Expires: Mon, 31 May 2021 00:45:17 GMT
Server: Apache
Content-Transfer-Encoding: binary
Cache-Control: max-age=31104000
Vary: Accept-Encoding
X-Ser: BC75_dx-lt-yd-zhejiang-jinhua-5-cache-10, BC144_dx-guangdong-jiangmen-7-cache-5
X-Cache: HIT from BC144_dx-guangdong-jiangmen-7-cache-5(baishan)
206 Partial Content

发送Range的请求 请求成功会返回206请求码

curl http://a4.att.hudong.com/21/09/01200000026352136359091694357.jpg -v -H "Range: bytes=0-1000"
curl http://a4.att.hudong.com/21/09/01200000026352136359091694357.jpg -v -H "Range: bytes=0-50, 100-150"
86183@DESKTOP-MKB5ERF MINGW64 ~/Desktop
$ curl http://a4.att.hudong.com/21/09/01200000026352136359091694357.jpg -H "Range: bytes=0-1000" -v
* STATE: INIT => CONNECT handle 0x288e150; line 1392 (connection #-5000)
* Added connection 0. The cache now contains 1 members
* STATE: CONNECT => WAITRESOLVE handle 0x288e150; line 1428 (connection #0)% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 183.47.233.138...
* TCP_NODELAY set
* STATE: WAITRESOLVE => WAITCONNECT handle 0x288e150; line 1509 (connection #0)
* Connected to a4.att.hudong.com (183.47.233.138) port 80 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x288e150; line 1561 (connection #0)
* Marked for [keep alive]: HTTP default
* STATE: SENDPROTOCONNECT => DO handle 0x288e150; line 1579 (connection #0)
> GET /21/09/01200000026352136359091694357.jpg HTTP/1.1
> Host: a4.att.hudong.com
> User-Agent: curl/7.58.0
> Accept: */*
> Range: bytes=0-1000
>
* STATE: DO => DO_DONE handle 0x288e150; line 1658 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x288e150; line 1783 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x288e150; line 1799 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 206 Partial Content
< Date: Fri, 05 Jun 2020 00:49:40 GMT
< Content-Type: image/jpg
< Content-Length: 1001
< Connection: keep-alive
< Expires: Mon, 31 May 2021 00:46:31 GMT
* Server Apache is not blacklisted
< Server: Apache
< Content-Transfer-Encoding: binary
< Cache-Control: max-age=31104000
< Vary: Accept-Encoding
< Content-Range: bytes 0-1000/253354
< X-Ser: BC75_dx-lt-yd-zhejiang-jinhua-5-cache-10, BC144_dx-guangdong-jiangmen-7-cache-5
< X-Cache: HIT from BC144_dx-guangdong-jiangmen-7-cache-5(baishan)

3xx

重定向,需要进一步的操作以完成请求

301 永久重定向
curl -I  http://publicobject.com/secrets/hellosecret.txtHTTP/1.1 301 Moved Permanently
Server: nginx/1.10.3 (Ubuntu)
Date: Wed, 23 Sep 2020 12:39:47 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: https://publicobject.com/secrets/hellosecret.txt
302 临时重定向
curl -X OPTIONS www.baidu.com  -IHTTP/1.1 302 Found
Connection: keep-alive
Content-Length: 17931
Content-Type: text/html
Date: Sun, 07 Jun 2020 01:58:52 GMT
Etag: "54d9748e-460b"
Server: bfe/1.0.8.18
304 Not Modified

访问的资源未被修改,使用缓存就OK

curl 'http://ask.android-studio.org/uploads/article/20150602/49e627d8b62d6c877c805ded9c2ec964.jpg' -IHTTP/1.1 200 OK
Date: Sun, 07 Jun 2020 01:40:09 GMT
Server: Apache/2.4.10 (Unix) OpenSSL/1.0.1j PHP/5.5.19 mod_perl/2.0.8-dev Perl/v5.16.3
Last-Modified: Tue, 02 Jun 2015 14:24:22 GMT
ETag: "ba74-51789b2d7d180"
Accept-Ranges: bytes
Content-Length: 47732
Content-Type: image/jpeg拿到ETag:"ba74-51789b2d7d180" 在请求的头部加上该Tag字段
curl 'http://ask.android-studio.org/uploads/article/20150602/49e627d8b62d6c877c805ded9c2ec964.jpg' -H 'If-None-Match: "ba74-51789b2d7d180"' -IHTTP/1.1 304 Not Modified
Date: Sun, 07 Jun 2020 01:41:41 GMT
Server: Apache/2.4.10 (Unix) OpenSSL/1.0.1j PHP/5.5.19 mod_perl/2.0.8-dev Perl/v5.16.3
ETag: "ba74-51789b2d7d180"
307 Temporary Redirect

临时重定向与302一样

curl -X OPTIONS developer.mozilla.org -IHTTP/1.1 307 Temporary Redirect
Server: CloudFront
Date: Sun, 07 Jun 2020 01:46:02 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://developer.mozilla.org/
X-Cache: Redirect from cloudfront
Via: 1.1 9e763d54b60e5dbe2c1faa8e75e52b67.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: NRT12-C3
X-Amz-Cf-Id: WsIx0xTqkkZuiQy3Ce3bK9y-Uij6cSmDp0MZxJSF4b_BVjfcz_DXJQ==这种也可以直接让命令帮你执行重定向后的地址
curl -X OPTIONS developer.mozilla.org -I -LHTTP/1.1 307 Temporary Redirect
Server: CloudFront
Date: Sun, 07 Jun 2020 01:47:57 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://developer.mozilla.org/
X-Cache: Redirect from cloudfront
Via: 1.1 263337573333cdedec0f11e424dd369d.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: NRT12-C3
X-Amz-Cf-Id: 4luQJXzVYxKWtaCPtEyCsOyhnBN_phk_tDYTUwdKZNtQfbMxTq1vnQ==HTTP/2 301
content-type: text/html; charset=utf-8
content-length: 0
cache-control: public, max-age=0, s-maxage=300
date: Sun, 07 Jun 2020 01:47:58 GMT
location: /en-US/
server: meinheld/1.0.1
strict-transport-security: max-age=63072000
vary: Cookie
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
x-cache: Miss from cloudfront
via: 1.1 0fde9b863333aeec4c82b26429369a68.cloudfront.net (CloudFront)
x-amz-cf-pop: NRT12-C3
x-amz-cf-id: -uitrYvfee2XWrskMrTc4gW7AZHSx3lxPS_SVvSfl57KuZW7G9PpDA==HTTP/2 200
content-type: text/html; charset=utf-8
content-length: 43024
vary: Accept-Encoding
cache-control: public, max-age=0, s-maxage=300
content-language: en-US
date: Sun, 07 Jun 2020 01:47:58 GMT
server: meinheld/1.0.1
strict-transport-security: max-age=63072000
x-content-type-options: nosniff
x-frame-options: DENY
x-xss-protection: 1; mode=block
x-cache: Miss from cloudfront
via: 1.1 0fde9b863333aeec4c82b26429369a68.cloudfront.net (CloudFront)
x-amz-cf-pop: NRT12-C3
x-amz-cf-id: PoQnt7oC9HwvYcXWC1DwdFZiTLAVZBHzZ2yxMmCeZ-UhS5YCNudYTA==

4xx

客户端错误,访问了服务端不支持方法或者不存在的资源

401 Unauthorized

表示未经许可,未认证的

curl -I  https://publicobject.com/secrets/hellosecret.txtHTTP/1.1 401 Unauthorized
Server: nginx/1.10.3 (Ubuntu)
Date: Wed, 23 Sep 2020 12:42:56 GMT
Content-Type: text/html
Content-Length: 204
Connection: keep-alive
WWW-Authenticate: Basic realm="OkHttp Secrets"

在网页上就是会弹窗出一个登录弹窗:

输入账号密码后登录成功,如下:

账号:jesse 密码:password1
ps:上述范例是OkHttp中的验证范例Authenticate.java

要求我们在参数中把账号密码传输过去,通过头部字段Authorization:

curl -I  https://publicobject.com/secrets/hellosecret.txt -H "Authorization: Basic amVzc2U6cGFzc3dvcmQx"HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Wed, 23 Sep 2020 12:53:30 GMT
Content-Type: text/plain
Content-Length: 1226
Last-Modified: Thu, 12 Jun 2014 01:53:57 GMT
Connection: keep-alive
ETag: "53990835-4ca"
Accept-Ranges: bytescurl https://publicobject.com/secrets/hellosecret.txt -H "Authorization: Basic amVzc2U6cGFzc3dvcmQx"@@@@@\@@@@@@@@@@@@@@@@@@@@@@@@@@@.@/ \@@@@@@@@@@@@@@@@.@   @@@@@@@@@@@@@@@@+@\ /@@@@@@@@"*@@/^@/\@@@@@@@@@@/  "   "@@@@@@@@@@@@@/:@@@..@@@@@@@:   +@@       `@@      @@`   @@     @@.@@@@'@@@@:  +@@       `@@      @@`   @@     @@@@@     @@@  +@@       `@@      @@`   @@     @@.@@       @@: +@@   @@@ `@@      @@` @@@@@@ @@@@@@  @@;@@@@@@@@       @@@ +@@  @@@  `@@      @@` @@@@@@ @@@@@@  @@@@@@@@@@@@       @@@ +@@ @@@   `@@@@@@@@@@`   @@     @@    @@@   :@@@@@       @@@ +@@@@@    `@@@@@@@@@@`   @@     @@    @@#    @@+@@@       @@@ +@@@@@+   `@@      @@`   @@     @@    @@:    @@#@@:     .@@` +@@@+@@   `@@      @@`   @@     @@    @@#    @@+@@@.   .@@@  +@@  @@@  `@@      @@`   @@     @@    @@@   ,@@@@@@@@@@@   +@@   @@@ `@@      @@`   @@@@   @@@@  @@@@#@@@@@@@@@@@    +@@   #@@ `@@      @@`   @@@@:  @@@@: @@'@@@@@@@:@@:@@:
405 Method Not Allowed

请求的方法服务器不支持

curl -X DELETE http://example.org -iHTTP/1.1 405 Method Not Allowed
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sun, 07 Jun 2020 01:56:17 GMT
Expires: Sun, 14 Jun 2020 01:56:17 GMT
Server: EOS (vny/044F)
Content-Length: 0
416 Requested Range Not Satisfiable

当请求头中带有Range字段,范围合法返回206 ,范围不合法返回416

curl http://a4.att.hudong.com/21/09/01200000026352136359091694357.jpg -H "Range: bytes=253354-2533540" -v
* STATE: INIT => CONNECT handle 0xd3e150; line 1392 (connection #-5000)
* Added connection 0. The cache now contains 1 members
* STATE: CONNECT => WAITRESOLVE handle 0xd3e150; line 1428 (connection #0)% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 183.47.233.135...
* TCP_NODELAY set
* STATE: WAITRESOLVE => WAITCONNECT handle 0xd3e150; line 1509 (connection #0)
* Connected to a4.att.hudong.com (183.47.233.135) port 80 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0xd3e150; line 1561 (connection #0)
* Marked for [keep alive]: HTTP default
* STATE: SENDPROTOCONNECT => DO handle 0xd3e150; line 1579 (connection #0)
> GET /21/09/01200000026352136359091694357.jpg HTTP/1.1
> Host: a4.att.hudong.com
> User-Agent: curl/7.58.0
> Accept: */*
> Range: bytes=253354-2533540
>
* STATE: DO => DO_DONE handle 0xd3e150; line 1658 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0xd3e150; line 1783 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0xd3e150; line 1799 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 416 Requested Range Not Satisfiable
< Date: Sun, 07 Jun 2020 02:04:13 GMT
< Content-Type: text/html
< Content-Length: 234
< Connection: keep-alive
* Server web cache is not blacklisted
< Server: web cache
< Expires: Sun, 07 Jun 2020 02:04:13 GMT
< X-Ser: BC144_dx-guangdong-jiangmen-7-cache-5
< X-Cache: HIT from BC144_dx-guangdong-jiangmen-7-cache-5(baishan)

5xx

服务器内部错误

常用用法

防盗链+数据统计 - Referer

Referer: https://www.baidu.com/?tn=88093251_41_hao_pg
Referer: https://www.baidu.com/more/
referer: https://v.qq.com/
referer: https://v.qq.com/channel/usuk

防盗链

Referer是指网页是谁发起的请求,是从哪个页面发起的
通过Referer字段可以屏蔽非法的请求来源,范例:QQ空间的图片,如果非法就返回一个default的图片
当然这种方式是不准确的,我们可以自己构造合法的参数绕过服务器检测

数据统计

Referer是指网页是谁发起的请求,是从哪个页面发起的
很好理解,服务器统计这些数据是用来统计流量来源,统计占比,分析流量入口变化情况,从而指定运营策略和方向等等
比如某个来源流量上升了,提升了10%,那么大数据就要分析为什么升了,是由哪些运营策略影响的,还是有新的需求灰度,或者说分析这个数据在灰度期间是不是正常的。
同理某个渠道的来源下降了,那么也要分析原因,是运营策略造成的,还是说代码存在BUG等,这些都是需要分析的

断点续传+多线程下载 - Range

对于文件可以在请求头中添加Range字段,告诉服务器我只请求某些范围的数据,从而实现断点续传和多线程下载功能。
这个算是任何下载功能模块的的基本功能

【HTTP】【HTTP协议分享】相关推荐

  1. iic通信原理_电子知识之IIC通信原理和协议分享

    IIC 的一些特征: 两条总线:串行数据总线(SDA)和串行时钟总线(SCL) 真正的多主机总线 连接到相同总线的ic数量只受到总线的最大电容400pF限制. 串行8位双向数据在标准模式下可达100K ...

  2. 使用og协议分享web页面到WhatsApp

    知乎: 什么是 Open Graph 标签?不懂你还做什么社交营销优化?! 往要分享的链接插入标签: APP端: let jsStr = `<meta property="og:tit ...

  3. 西门子PLC几种常见的通讯协议分享

    一.PPI通讯:是西门子公司专为s7-200系列plc开发的通讯协议.内置于s7-200CPU中.PPI协议物理上基于RS-485口,通过屏蔽双绞线就可以实现PPI通讯.PPI协议是一种主-从协议.主 ...

  4. EIA-CEA-861-D协议分享(免费)

    https://pan.baidu.com/s/1oIhTUm4dc3ZtKCohxvH3bA k3j8

  5. 如何为你的代码选择一个开源协议

    相信很多刚踏入软件这个行业的小伙伴一如当初的我,对开源软件的各种协议不甚了解被搞昏了头脑.毕竟对于一个新生程序员来说,如何写好代码才是亟待解决的问题,无暇了解这些.随着你项目做得多了代码写得多了,你会 ...

  6. 【Git】认识各种开源协议及其关系

    开源许可协议 License是软件的授权许可,里面详尽表述了你获得代码后拥有的权利,可以对别人的作品进行何种操作,何种操作又是被禁止的. 开源协议的种类 现今存在的开源协议很多,而经过Open Sou ...

  7. P2P下载技术-BT协议与Magnet磁力链接

    P2P下载技术-BT协议与Magnet磁力链接 前言 传统C/S服务器-客户端文件下载 FTP下载 HTTP下载 C/S下载模式缺点 P2P下载 最早的P2P下载应用-Napster BT-BitTo ...

  8. 每周分享第 35 期

    这里记录过去一周,我看到的值得分享的东西,每周五发布. 欢迎投稿,或推荐你自己的项目,请前往 GitHub 的 ruanyf/weekly 提交 issue. 媒体报道,50岁以上的日本男性每四人就有 ...

  9. 谈BitTorrent协议中的DHT方法

    背景:P2P.BT协议 1.P2P方法的思想和产生原因 P2P全称Peer to Peer.要想理解p2p首先要了解另一种更原始的结构:C/S结构. C/S结构中,C指client,S指server. ...

最新文章

  1. matlab dir datenum,matlab中的datenum
  2. mysql alert on delete cascade_如何在mysql中删除级联使用?(How do I use on delete cascade in mysql?)...
  3. 开源JVM Sampling Profiler
  4. 关于ubuntu终端命令路径太长的问题
  5. 【LeetCode笔记(水)】s = null 与 s.length() == 0
  6. JavaScript操作DropDownList(Set value to dropdownlist with JavaScript)
  7. Top 10 DTrace scripts for Mac OS X
  8. linux 空信息,为什么recv()函数收到空消息?
  9. tensorflow之卷积池化和全连接
  10. 计算机网络结构与数据交换技术
  11. java 最长 几个字节,int类型占几个字节_在java中,int类型占四个字节,而byte类型占一个字节...
  12. php 图片扣背景透明,php 处理透明背景的图片时的问题
  13. 软件测试非功能性需求,什么是非功能测试?非功能测试包括哪些类型?
  14. 工商银行发消息说5星级服务器,工商银行5星级客户多吗?会刷星你也可以..
  15. 前端的CSP CSP如何落地,了解一下
  16. 外卖跑腿app开发功能分析
  17. 常用JS效果 需要时更新。。。
  18. codeblocks下如何修改新建文件的默认代码?
  19. 推荐专业IT电子书下载网站-https://itbook.download/
  20. 上海市水电煤热线电话一览

热门文章

  1. cPanel主机空间安装Magento规范准确详细教程
  2. oracle12c 创建数据库
  3. 怎么在计算机中添加电脑桌面,如何实现在电脑桌面上添加文字提醒
  4. 做销售的要背下来!太经典了!
  5. GD32F450创建工程模板
  6. [SV]SystemVerilog Mailbox
  7. springboot2.x+dubbo2.x+seata1.x AT+nacos+zk实现分布式事务
  8. Java 支付宝对账功能(查询+文件下载+解压+遍历文件+读文件)
  9. 如何在Science、Nature等国际顶刊发文,分子对接、深度学习基因组学,分子动力学、单细胞测序复现文章
  10. 产品经理工作相关软件