本篇只是自己学习过程中的一个记录
首先需要引入”net/http”包
import "net/http"
最简单的Get和Post请求
GET
resp,err := http.Get("http://blog.csdn.net/zyjying520/article/details/52669538") if err != nil { return } defer resp.Body.Close() // 一次性读取 bs,err := IoUtil.ReadAll(resp.Body) if err != nil { fmt.Println("错误") return } fmt.Println(string(bs))
POST
val := url.Values{} val.Set("name","vvvv") val.Add("a","bb") // Set Add 都可以 resp,err := http.PostForm("http://blog.csdn.net/zyjying520/article/details/52669538",val) if err != nil { fmt.Println("错误") return } defer resp.Body.Close() // 一次性读取 bs,err := IoUtil.ReadAll(resp.Body) if err != nil { fmt.Println("错误") return } fmt.Println(string(bs))
自定义Request
req,err := http.NewRequest(http.MethodGet,"http://blog.csdn.net/zyjying520/article/details/52669538",nil) req.Header.Set("xxx","") client := &http.Client{} resp,err := client.Do(req) if err != nil { fmt.Println(err) return } defer resp.Body.Close() bs,err := IoUtil.ReadAll(resp.Body) if err != nil { fmt.Println(err) return } fmt.Println(string(bs))原文链接:https://www.f2er.com/go/187678.html