http
- 使用
http
客户端发送请求 - 使用
http.Client
控制请求头部 - 使用
httputil
简化工作
func main() {
resp, err := http.Get("http://www.baidu.com")
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)
}
func main() {
req,err :=http.NewRequest(http.MethodGet,"http://www.baidu.com",nil)
req.Header.Add("User-Agent","Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1")
resp, err :=http.DefaultClient.Do(req)
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)
}
client := http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
fmt.Println(req)
return nil
}}
resp, err := client.Do(req)
http服务器性能分析
import _ "net/http/pprof
- 访问
/debug/pprof/
- 使用
go tool pprof
分析性能
import (
"log"
"net/http"
_ "net/http/pprof"
"os"
"imooc.com/ccmouse/learngo/lang/errhandling/filelistingserver/filelisting"
)
- 然后就可以打开网页对应端口的页面看到一些性能的分析
http://localhost:8888/debug/pprof/
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200605151704336.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjAxMjkxMQ==,size_16,color_FFFFFF,t_70)
- 额外功能点开
pprof
的文件,里面有一些命令 可以获取对应的数据,然后生成图,可以直观的看到性能消耗在哪里
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200605152303390.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjAxMjkxMQ==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200605152627170.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjAxMjkxMQ==,size_16,color_FFFFFF,t_70)
- 性能分析图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200605152603115.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjAxMjkxMQ==,size_16,color_FFFFFF,t_70)
其他标准库
- 可以通过命令行查看标准库
godoc -http :8080
, 然后访问http://localhost:8080/pkg/ - 官方标准库,中文版