一.建立一个简单的tcp服务器,来看看浏览器访问服务器的时候会发送什么信息。
package main
import (
"fmt"
"net"
)
func main() {
listener,err := net.Listen("tcp","127.0.0.1:8080")
if err!=nil{
fmt.Println("net.Listen err",err)
return
}
for{
con,err := listener.Accept()
if err!=nil{
fmt.Println("listener.Accept err",err)
continue
}
buf := make([]byte,4096)
n,err := con.Read(buf)
if err!=nil{
fmt.Println("listener.Accept err",err)
continue
}
fmt.Printf("---\n%s\n---",buf[:n])
}
}
用浏览器访问127.0.0.1:8080,后台打印结果是:
GET / HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
sec-ch-ua: "Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"
sec-ch-ua-mobile: ?0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
二.创建一个http服务器,再创建一个客户端去访问http服务器,看看服务器响应报文是啥样的
- http服务器
package main
import (
"net/http"
)
type myHandler struct{}
func (h myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request){
w.Write([]byte("hello go"))
}
func main() {
http.Handle("/go",myHandler{})
http.ListenAndServe(":8080",nil)
}
- 客户端
reqBuf的内容就是第一步获取到的请求报文
package main
import (
"fmt"
"net"
)
func main() {
con,err := net.Dial("tcp",":8080")
if err!=nil{
fmt.Printf("net.Dial err : %s\n",err)
return
}
defer con.Close()
reqBuf :="GET /go HTTP/1.1\nHost: 127.0.0.1:8080\nConnection: keep-alive\nsec-ch-ua: \"Google Chrome\";v=\"87\", \" Not;A Brand\";v=\"99\", \"Chromium\";v=\"87\"\nsec-ch-ua-mobile: ?0\nUpgrade-Insecure-Requests: 1\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\nSec-Fetch-Site: none\nSec-Fetch-Mode: navigate\nSec-Fetch-User: ?1\nSec-Fetch-Dest: document\nAccept-Encoding: gzip, deflate, br\nAccept-Language: zh-CN,zh;q=0.9\n\n"
n,err2 := con.Write([]byte(reqBuf))
if err2!=nil{
fmt.Printf("con.Write err : %s\n",err2)
return
}
buf := make([]byte,1<<11)
n,err = con.Read(buf)
if err!=nil{
fmt.Printf("net.Dial err : %s\n",err)
return
}
fmt.Println(string(buf[:n]))
}
开启服务器,运行客户端得到的响应报文:
package main
import (
"fmt"
"net"
)
func main() {
con,err := net.Dial("tcp",":8080")
if err!=nil{
fmt.Printf("net.Dial err : %s\n",err)
return
}
defer con.Close()
reqBuf :="GET /go HTTP/1.1\nHost: 127.0.0.1:8080\nConnection: keep-alive\nsec-ch-ua: \"Google Chrome\";v=\"87\", \" Not;A Brand\";v=\"99\", \"Chromium\";v=\"87\"\nsec-ch-ua-mobile: ?0\nUpgrade-Insecure-Requests: 1\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\nSec-Fetch-Site: none\nSec-Fetch-Mode: navigate\nSec-Fetch-User: ?1\nSec-Fetch-Dest: document\nAccept-Encoding: gzip, deflate, br\nAccept-Language: zh-CN,zh;q=0.9\n\n"
n,err2 := con.Write([]byte(reqBuf))
if err2!=nil{
fmt.Printf("con.Write err : %s\n",err2)
return
}
buf := make([]byte,1<<11)
n,err = con.Read(buf)
if err!=nil{
fmt.Printf("net.Dial err : %s\n",err)
return
}
fmt.Println(string(buf[:n]))
}