使用NC模拟HTTP请求

博文背景

很多人使用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&amp;web_id=1260845656&amp;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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值