NC 发送HTTP 报文请求

查找DNS地址

首先查找httpbin.org 的dns 地址

1
2
3
4
5
6
7
8
9
nslookup httpbin.org
Server: 10.255.255.254
Address: 10.255.255.254#53

Non-authoritative answer:
Name: httpbin.org
Address: 18.213.123.165
Name: httpbin.org
Address: 34.224.200.202

nc 使用HTTP/1.0 协议与httpbin.org 进行报文通讯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
❯ nc -v 18.213.123.165 80

Connection to 18.213.123.165 80 port [tcp/http] succeeded!
GET /get HTTP/1.0


HTTP/1.1 200 OK
Date: Tue, 03 Dec 2024 02:22:19 GMT
Content-Type: application/json
Content-Length: 317
Connection: close
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
"args": {},
"headers": {
"Host": "k8s-pmhttpbi-pmhttpbi-4b5623588d-1738525105.us-east-1.elb.amazonaws.com",
"X-Amzn-Trace-Id": "Root=1-674e6b5b-33e7909f46b99703252571c3"
},
"origin": "128.1.97.123",
"url": "http://k8s-pmhttpbi-pmhttpbi-4b5623588d-1738525105.us-east-1.elb.amazonaws.com/get"
}

  • -v 参数是打印详细信息
  • GET /get HTTP/1.0 是自己手动输入的
  • Enter 按两次

使用echo 命令转义字符

1
echo -e "GET /get HTTP/1.0\r\n" | nc  18.213.123.165 80

nc 使用HTTP/1.1 协议与httpbin.org 进行报文通讯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
❯ nc -v 18.213.123.165 80

Connection to 18.213.123.165 80 port [tcp/http] succeeded!
GET /get HTTP/1.1
HOST: httpbin.org

HTTP/1.1 200 OK
Date: Tue, 03 Dec 2024 02:27:10 GMT
Content-Type: application/json
Content-Length: 197
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
"args": {},
"headers": {
"Host": "httpbin.org",
"X-Amzn-Trace-Id": "Root=1-674e6c7e-1299f1db390d3827443ff03b"
},
"origin": "128.1.97.123",
"url": "http://httpbin.org/get"
}

1
echo -e "GET /get HTTP/1.1\r\nHost: httpbin.org\r\n" | nc -v 18.213.123.165 80

使用nc 发送POST 请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
curl -X POST http://httpbin.org/post -d "key=value&key2=value2"

{
"args": {},
"data": "",
"files": {},
"form": {
"key": "value",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Content-Length": "21",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.81.0",
"X-Amzn-Trace-Id": "Root=1-674e7914-6f0d751e2b96f59a18092076"
},
"json": null,
"origin": "107.175.218.218",
"url": "http://httpbin.org/post"
}

  1. 直接使用 get 中的方式
1
2
3
echo -e "POST /post HTTP/1.0\r\n\r\nkey1=value1&key2=value2" | nc -v 18.213.123.165 80

# 响应没有解析出来
  1. 直接使用表单格式

    1
    2
    echo -e "POST /post HTTP/1.0\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\nkey1=value1&key2=value2" | nc -v 18.213.123.165 80
    # 没有解析出来
  2. 使用 Content-Length + form

1
2
3
4
5
6
echo -e "POST /post HTTP/1.0\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 23\r\n\r\nkey1=value1&key2=value2" | nc -v 18.213.123.165 80

#"form": {
# "key1": "value1",
# "key2": "value2"
#}
  1. 只使用Content-Length
1
echo -e "POST /post HTTP/1.0\r\nContent-Length: 23\r\n\r\nkey1=value1&key2=value2" | nc -v 18.213.123.165 80

​ 只在data 中有值

1
"data": "key1=value1&key2=value2"

使用nc 发送HEAD请求

1
echo "HEAD / HTTP/1.0\r\n" | nc -v 18.213.123.165 80

NC 发送HTTP 报文请求
https://kingjem.github.io/2024/12/03/nc 发送请求/
作者
Ruhai
发布于
2024年12月3日
许可协议