博文背景
很多人使用telnet来模拟HTTP请求,我总觉得它不应该这么用,而是用NC更具有正规血统,可以帮助我们从本质上理解HTTP数据包
具体操作
1.使用HTTP/1.0
ubuntu@guangdong:~ $ nc httpbin.org 80
GET /get HTTP/1.0
HTTP/1.1 200 OK
Date: Wed, 22 Apr 2020 11:03:46 GMT
Content-Type: application/json
Content-Length: 316
Connection: close
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {},
"headers": {
"Host": "a0207c42-pmhttpbin-pmhttpb-c018-592832243.us-east-1.elb.amazonaws.com",
"X-Amzn-Trace-Id": "Root=1-5ea02492-04d3ef1b0fd958c7cf16dd14"
},
"origin": "203.195.161.196",
"url": "http://a0207c42-pmhttpbin-pmhttpb-c018-592832243.us-east-1.elb.amazonaws.com/get"
}
注意:内容必须以两个空行结尾。这符合rfc2616的标准,否则不会被正确解码的。
2.使用HTTP/1.1
注意:Host头处理,在HTTP1.0中认为每台服务器都绑定一个唯一的IP地址,因此,请求消息中的URL并没有传递主机名(hostname)。但随着虚拟主机技术的发展,在一台物理服务器上可以存在多个虚拟主机(Multi-homed Web Servers),并且它们共享一个IP地址。HTTP1.1的请求消息和响应消息都应支持Host头域,且请求消息中如果没有Host头域会报告一个错误(400 Bad Request)。
ubuntu@guangdong:~ $ nc www.zstu.edu.cn 80
GET / HTTP/1.1
Host: www.zstu.edu.cn
HTTP/1.1 200 OK
Date: Wed, 22 Apr 2020 11:25:50 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 66850
Connection: keep-alive
Vary: Accept-Encoding
Server: VWebServer
Last-Modified: Mon, 20 Apr 2020 11:59:23 GMT
ETag: "10522-5a3b7a13c9cc0"
Accept-Ranges: bytes
Vary: User-Agent,Accept-Encoding
Cache-Control: private, max-age=600
Expires: Wed, 22 Apr 2020 11:25:23 GMT
Content-Language: zh-CN
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>浙江理工大学</title><META Name="keywords" Content="浙江理工大学" />
<script type="text/javascript" src="https://s84.cnzz.com/stat.php?id=1260845656&web_id=1260845656&show=pic" async="false"></script><script type="text/javascript" src="https://hm.baidu.com/hm.js?3a8c303cfc611b527f4ba8a41b4a69ab" async="true"></script>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<link rel="stylesheet" type="text/css" href="dfiles/11508/template/default/css/global.css">
<link rel="stylesheet" type="text/css" href="dfiles/11508/template/default/css/style.css">
<script type="text/javascript" src="dfiles/11508/template/default/js/jquery.js">
</script><script type="text/javascript" src="dfiles/11508/template/default/js/dd_belatedpng_0.0.8a-min.js"></script>