1.tcp的客户端,并且直接读取http协议的全部内容,每次读取4096字节,直到最后一个字节是n并且读取的长度小于4096
代码语言:javascript复制 conn, err := net.Dial("tcp", "www.baidu.com:80")
if err != nil {
log.Fatal(err)
}
_, err = conn.Write([]byte("GET / HTTP/1.1rnrn"))
if err != nil {
log.Fatal(err)
}
var httpBody string
var httpSize int
for {
b := make([]byte, 4096)
len, err := conn.Read(b)
if err != nil {
log.Fatal(err)
}
httpBody = string(b[:len])
httpSize = len
if b[len-1] == 'n' && len < 4096 {
break
}
}
log.Println(httpBody, httpSize)