//发送http请求 package main import ( "fmt" "net" "io" ) func main () { //使用Dial建立连接 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: close\r\n" msg += "\r\n\r\n" _, err = io.WriteString(conn, msg) if err != nil { fmt.Println("write string failed", err) return } buf := make([]byte, 4096) for { count, err := conn.Read(buf) if err != nil { break } fmt.Println(string(buf[0:count])) } }
原文地址:https://www.cnblogs.com/zhangxiaoj/p/11279061.html
时间: 2024-10-29 04:47:12