Go http 库

 

go http 库基本用法

0x01 最简单的 http client.

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 复杂一点的


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

参考和抄袭: