一.建立一個(gè)簡(jiǎn)單的tcp服務(wù)器厚骗,來看看瀏覽器訪問服務(wù)器的時(shí)候會(huì)發(fā)送什么信息侄柔。
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,后臺(tái)打印結(jié)果是:
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
二.創(chuàng)建一個(gè)http服務(wù)器濒析,再創(chuàng)建一個(gè)客戶端去訪問http服務(wù)器,看看服務(wù)器響應(yīng)報(bào)文是啥樣的
- http服務(wù)器
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的內(nèi)容就是第一步獲取到的請(qǐng)求報(bào)文
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]))
}
開啟服務(wù)器啥纸,運(yùn)行客戶端得到的響應(yīng)報(bào)文:
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]))
}