美文网首页
【golang】http请求报文和响应报文

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

作者: 七八个星天 | 来源:发表于2021-01-13 12:22 被阅读0次
一.建立一个简单的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]))
}

相关文章

  • HTTP协议报文及Chrome Network常用功能

    HTTP协议报文 Http协议报文分为【Http请求报文】和【Http响应报文】 HTTP请求报文 Http请求报...

  • Android 基础之网络

    HTTP Http报文 http报文可以分为请求报文和响应报文。请求报文格式: 响应报文格式: 解释下各个标签: ...

  • 3. HTTP报文内的HTTP信息

    HTTP报文 请求报文 | 响应报文分为报文头部和报文主体 请求报文和响应报文的结构 报文头部内容如下: 请求行 ...

  • http报文解析, 网络通信协议类型,cookie,token,

    HTTP报文: HTTP报文的流动方向: 报文的语法: 1.1请求报文: 请求报文的格式: 1.2响应报文: 响应...

  • 彻底搞懂 HTTP 报文、报文信息以及 HTTP 状态码

    一、HTTP 报文 用于HTTP协议交互的信息被称为HTTP报文, HTTP 报文分为请求报文和响应报文; 请求报...

  • HTTP message

    HTTP message HTTP 报文有两种形式: 请求报文形式和响应报文形式.请求报文和响应报文都有如同下面的...

  • http报文结构解析

    1、http协议结构? 2、请求报文和响应报文的区别是? 请求报文和响应报文的区别在于,请求报文是请求行,相应报文...

  • HTTP报文内的HTTP信息

    HTTP报文   用于HTTP协议交互的信息被称为HTTP报文。请求端的HTTP报文叫做请求报文,响应段的叫做响应...

  • 网络请求相关

    HTTP 超文本传输协议 请求/响应报文 连接建立流程 HTTP的特点 1. 请求/响应报文 请求报文请求报文包括...

  • 网络

    HTTP HTTP是一个超文本传输协议 构成:请求报文&响应报文 请求报文 响应报文 HTTP的请求方式有哪些? ...

网友评论

      本文标题:【golang】http请求报文和响应报文

      本文链接:https://www.haomeiwen.com/subject/ayvsaktx.html