【golang】http请求报文和响应报文

七八个星天 · · 1184 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

一.建立一个简单的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]))
}

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:七八个星天

查看原文:【golang】http请求报文和响应报文

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1184 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传