使用curl
命令发起请求
curl -s -v -- "www.baidu.com" 使用curl发起GET请求
复制代码
curl -X post -s -v -- "www.baidu.com" 使用curl发起POST请求
复制代码
会得到这样的以下两大段内容
* Rebuilt URL to: www.baidu.com/
* Trying 14.215.177.39...
* TCP_NODELAY set
* Connected to www.baidu.com (14.215.177.39) port 80 (#0)
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.55.1
> Accept: */*
>
< 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: Thu, 03 Jun 2021 14:18:13 GMT
< Etag: "588604dc-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:56 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>……</html>
* Connection #0 to host www.baidu.com left intact
复制代码
* Rebuilt URL to: www.baidu.com/
* Trying 14.215.177.38...
* TCP_NODELAY set
* Connected to www.baidu.com (14.215.177.38) port 80 (#0)
> post / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 302 Found
< Content-Length: 17931
< Content-Type: text/html
< Date: Thu, 03 Jun 2021 14:43:56 GMT
< Etag: "54d9748e-460b"
< Server: bfe/1.0.8.18
<
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<style data-for="result" id="css_result">
body{color:#333;background:#fff;padding:6px 0 0;margin:0;position:relative;min-width:900px}body,th,td,.p1,.p2{font-family:arial}p,form,ol,ul,li,dl,dt,dd,h3{margin:0;padding:0;list-style:none}input{padding-top:0;padding-bottom:0;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}table,img{border:0}td{font-size:9pt;line-height:18px}
...<span>此内容系百度根据您的指令自动搜索的结果,不代表
百度赞成被搜索网站的内容或立场</span></span>
<span id="help"><a href="http://www.baidu.com/search/jubao.html" target="_blank">举报</a></span></div>
<div class="c-tips-container" id="c-tips-container"></div></div></div><div class="c-tips-container" id="c-tips-container"></div>
</body></html>* Connection #0 to host www.baidu.com left intact
复制代码
以上两大段内容中
- 带
*
的部分是注释 - 带
>
的部分是发出去请求 - 带
<
的部分是获得的响应
去除掉注释部分,我们可以看到请求与响应的基本格式
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.55.1
> Accept: */*
>
----
> post / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.55.1
> Accept: */*
>
复制代码
请求格式
> 动词 路径 协议/版本 第一部分,请求行
> key1: value1 第二部分,请求头部,由一对对键值对组成
> key2: value2
> Host: baidu.com
> User-Agent: curl/7.55.1
> Accept: */*
> 第三部分,空行,只有一个换行符,用来区分第二四部分
> 第四部分,请求数据,可为空
复制代码
- 请求最多包含4部分,最少包含3部分,第4部分可为空
- 其中第一部分的 “动词” 有
GET
POST
PUT
全部更新替换PATCH
部分更新DELETE
删除OPTION
HEAD
CONNECT
TRACE
- 路径包含 “查询参数” ,但不包含锚点
- 未写路径,默认为
/
根目录 - 第二部分中的
content-Type
标注了第4部分的格式
响应格式
< HTTP/1.1 302 Found
< Content-Length: 17931
< Content-Type: text/html
< Date: Thu, 03 Jun 2021 14:43:56 GMT
< Etag: "54d9748e-460b"
< Server: bfe/1.0.8.18
<
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
……
</html>
复制代码
< 协议/版本 状态码 状态解释 第一部分,状态行
< Content-Length: 17931 第二部分,由键值对组成
< Content-Type: text/html
< Date: Thu, 03 Jun 2021 14:43:56 GMT
< Etag: "54d9748e-460b"
< Server: bfe/1.0.8.18
< 第三部分,空行
<html> 第四部分,响应正文
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
……
</html>
复制代码
- 第二部分的
Content-Type
标注了第4部分的格式,遵循MIME规范 - 状态码
HTTP 响应状态码用来表明特定 HTTP 请求是否成功完成。 响应被归为以下五大类:
1XX
(消息)请求已被接受,需要请求者继续操作(不常用)2XX
(成功)操作被成功接受并处理3XX
(重定向)需要进一步操作以完成请求4XX
(客户端错误)请求包含语法错误或者无法完成请求5XX
(服务器错误)服务器处理请求过程中出现错误
常见请求 | |
---|---|
100 | 目前所有内容都是可行的,客户端应该继续请求,如果已经完成,则忽略它 |
102 | 服务器已收到并正在处理该请求,但当前没有响应可用 |
200 | 请求成功 |
201 | 请求已成功,且创建了一个新的资源。通常是在 POST 或某些 PUT 请求之后返回的响应 |
202 | 请求已经接收到,但还未响应,没有结果 |
204 | 服务器成功处理,但未返回内容。创建成功(POST请求) |
301 | 请求资源永久移到新的URL,新的URL在响应的Location域中返回 |
302 | 资源临时移动,同样的新的临时URL在响应的Location域中返回 |
304 | 所有请求资源未修改,服务器不会返回任何资源 |
403 | 服务器已理解请求,但是拒绝执行 |
404 | 请求失败,请求所希望得到的资源在服务器上并未找到 |
500 | 服务器内部错误,无法完成请求 |
501 | 服务器不支持请求方法,因此无法处理 |
502 | 作为网关或者代理工作的服务器尝试执行请求时,从远程上游服务器接收到无效的请求 |
503 | 服务器临时维护或过载,无法处理请求 |
使用Chrome开发者工具查看 HTTP 请求和响应内容
- 打开浏览器按快捷键
ctrl
+shift
+i
打开开发者工具页面 - 点击
Network
- 在地址栏输入网址
- 查看
Request
再点击View source
5. 就可以看到请求的前三部分,如果有请求的第四部分,在
FormData
或Payload
里可以看到