饥人谷技术博客让前端飞程序员

HTTP入门介绍 : HTTP请求和响应 &&

2018-11-28  本文已影响39人  小7丁

HTTP 请求

curl -s -v -H "XJJ: xxx" -- "https://www.baidu.com" // 默认GET请求
curl -X POST -s -v -H "XJJ: xxx" -- "https://www.baidu.com" // 改成POST请求
curl -X POST -d "1234567890" -s -v -H "Frank: xxx" -- "https://www.baidu.com"  // 改成带数据的POST请求

这个命令的解释可以这里看

image
在终端中>有大于号在前面的都是请求的内容,而<小于号在前面的都是响应的内容(原谅我截图里面的错别字,我上班写的博客懒得再改了--!!!) image
GET / HTTP/1.1
Host: www.baidu.com
User-Agent: curl/7.54.0
Accept: */*
XJJ: xxx

POST / HTTP/1.1
Host: www.baidu.com
User-Agent: curl/7.54.0
Accept: */*
XJJ: xxx
Content-Length: 10
Content-Type: application/x-www-form-urlencoded

1234567890

把上面的内容抽离成下面的格式:

1 动词 路径 协议/版本
2 Key1: value1
2 Key2: value2
2 Key3: value3
2 Content-Type: application/x-www-form-urlencoded
2 Host: www.baidu.com
2 User-Agent: curl/7.54.0
3 
4 要上传的数据

请求最多可以分为四个部分,最少为三个部分:

  1. 第三部分永远是一个回车
  2. 动词内包含GET POST PUT PATCH DELETE HEAD OPTION
  3. 这里的路径包括查询参数,但不包括锚点
  4. 若没有写路径,默认/
  5. 第 2 部分中的 Content-Type 标注了第 4 部分的格式

Chrome开发者工具查看HTTP请求

image

HTTP 响应

上面的请求示例返回的响应内容分别为:

HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: Keep-Alive
Content-Length: 2443
Content-Type: text/html
Date: Wed, 28 Nov 2018 06:27:25 GMT
Etag: "58860415-98b"
Last-Modified: Mon, 23 Jan 2017 13:24:37 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> 后面太长,省略了……
HTTP/1.1 302 Found
Connection: Keep-Alive
Content-Length: 17931
Content-Type: text/html
Date: Wed, 28 Nov 2018 06:27:25 GMT
Etag: "54d9749e-460b"
Server: bfe/1.0.8.18

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"> 后面太长,省略了……

把上面的内容抽离成下面的格式:

1 协议/版本号 状态码 状态解释
2 Key1: value1
2 Key2: value2
2 Content-Length: 17931
2 Content-Type: text/html
3
4 要下载的内容

第 2 部分中的 Content-Type 标注了第 4 部分的格式

Chrome开发者工具查看HTTP请求

image

如图

上一篇 下一篇

猜你喜欢

热点阅读