Post

Go http 库

Go http 库

go http 库基本用法

0x01 最简单的 http client.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import (
    "net/http"
    "net/http/httputil"
)

func main() {
    resp, err := http.Get("https://www.chaos.luxe/")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    s, err := httputil.DumpResponse(resp, true)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%s\n", s)

}

0x02 复杂一点的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import (
    "net/http"
    "net/http/httputil"
)

func main() {
    request, err := http.NewRequest(http.MethodGet, "https://www.chaos.luxe", nil)
    request.Header()
    
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    s, err := httputil.DumpResponse(resp, true)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%s\n", s)

}

Conclusion

参考和抄袭:

This post is licensed under CC BY 4.0 by the author.