3.tcp获取网页数据
最后更新于:2022-04-02 04:46:18
tcp获取网站数据
~~~
package main
import (
"fmt"
"io"
"net"
)
func main() {
conn, err := net.Dial("tcp", "www.baidu.com:80")
if err != nil {
fmt.Println("Error dialing", err.Error())
return
}
defer conn.Close()
msg := "GET / HTTP/1.1\r\n"
msg += "Host:www.baidu.com\r\n"
msg += "Connection:keep-alive\r\n"
//msg += "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36\r\n"
msg += "\r\n\r\n"
//io.WriteString(os.Stdout, msg)
n, err := io.WriteString(conn, msg)
if err != nil {
fmt.Println("write string failed, ", err)
return
}
fmt.Println("send to baidu.com bytes:", n)
buf := make([]byte, 4096)
for {
count, err := conn.Read(buf)
fmt.Println("count:", count, "err:", err)
if err != nil {
break
}
fmt.Println(string(buf[0:count]))
}
}
~~~
运行结果:
~~~
$ go run main.go
send to baidu.com bytes: 63
count: 3054 err:
HTTP/1.1 200 OK
Date: Thu, 22 Mar 2018 09:26:58 GMT
Content-Type: text/html
Content-Length: 14615
Last-Modified: Tue, 20 Mar 2018 02:53:00 GMT
Connection: Keep-Alive
Vary: Accept-Encoding
Set-Cookie: BAIDUID=BFCA28D8B6D8FA0920CE394CC3502EA9:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=BFCA28D8B6D8FA0920CE394CC3502EA9; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1521710818; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Server: BWS/1.1
X-UA-Compatible: IE=Edge,chrome=1
Pragma: no-cache
Cache-control: no-cache
Accept-Ranges: bytes
百度一下,你就知道
count: 0 err: EOF
~~~
';