net/http包提供了HTTP客户端和服务端的实现
超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络传输协议
所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。

1.HTTP客户端

1.2GET请求

  1. resp, err := http.Get("https://www.bilibili.com/")
  2. if err != nil {
  3. fmt.Printf("get failed,err:%v\n",err)
  4. return
  5. }
  6. defer resp.Body.Close()
  7. body, err := ioutil.ReadAll(resp.Body)
  8. if err != nil {
  9. fmt.Printf("read failed,err:%v\n",err)
  10. return
  11. }
  12. fmt.Println(string(body))

1.3带参数的GET请求

关于GET请求的参数需要使用Go语言内置的net/url这个标准库来处理

  1. import (
  2. "fmt"
  3. "io/ioutil"
  4. "net/http"
  5. "net/url"
  6. )
  7. func main() {
  8. apiUrl := "http://127.0.0.1:9000/get"
  9. u, err := url.ParseRequestURI(apiUrl)
  10. if err != nil {
  11. fmt.Printf("parse failed,err:%v\n",err)
  12. }
  13. fmt.Printf("%T\n",u) //*url.URL
  14. fmt.Println(u) //http://127.0.0.1:9000/get
  15. data := url.Values{}
  16. data.Set("name","cheng")
  17. data.Set("age","18")
  18. //encode := data.Encode()
  19. //fmt.Println(encode) //age=18&name=cheng
  20. u.RawQuery = data.Encode()
  21. fmt.Println(u) //http://127.0.0.1:9000/get
  22. resp, err := http.Get(u.String())
  23. if err != nil {
  24. fmt.Printf("get failed,err:%v",err)
  25. return
  26. }
  27. defer resp.Body.Close()
  28. body, err := ioutil.ReadAll(resp.Body)
  29. if err != nil {
  30. fmt.Printf("get failed,err:%v",err)
  31. return
  32. }
  33. fmt.Println(string(body))
  34. }

对应的Server端HandlerFunc

  1. func getHandler(w http.ResponseWriter, r *http.Request) {
  2. defer r.Body.Close()
  3. data := r.URL.Query()
  4. fmt.Println(data.Get("name"))
  5. fmt.Println(data.Get("age"))
  6. answer := `{"status": "ok"}`
  7. w.Write([]byte(answer))
  8. }

2.Post请求

  1. url := "http://127.0.0.1:9090/post"
  2. contentType := "application/json"
  3. data := `{name:"cheng",age:"18"}`
  4. resp, err := http.Post(url, contentType, strings.NewReader(data))
  5. if err != nil {
  6. fmt.Printf("post failed,err:%v\n",err)
  7. return
  8. }
  9. defer resp.Body.Close()
  10. body, err := ioutil.ReadAll(resp.Body)
  11. if err != nil {
  12. fmt.Printf("get failed,err:%v\n",err)
  13. return
  14. }
  15. fmt.Println(string(body))
  1. url := "http://www.bilibili.com/"
  2. contentType := "application/json"
  3. data := ``
  4. resp, err := http.Post(url, contentType, strings.NewReader(da
  5. if err != nil {
  6. fmt.Printf("post failed,err:%v\n",err)
  7. return
  8. }
  9. defer resp.Body.Close()
  10. body, err := ioutil.ReadAll(resp.Body)
  11. if err != nil {
  12. fmt.Printf("get failed,err:%v\n",err)
  13. return
  14. }
  15. fmt.Println(string(body))

对应的Server端HandlerFunc

  1. func postHandler(w http.ResponseWriter, r *http.Request){
  2. defer r.Body.Close()
  3. r.ParseForm()
  4. fmt.Println(r.PostForm) //打印form数据
  5. fmt.Println(r.PostForm.Get("name"),r.PostForm.Get("age"))
  6. body, err := ioutil.ReadAll(r.Body)
  7. if err != nil {
  8. fmt.Printf("read failed,err:%v",err)
  9. return
  10. }
  11. fmt.Println(string(body))
  12. answer := `{"status":"ok"}`
  13. w.Write([]byte(answer))
  14. }

3.默认的Server

net/http包是对net包的进一步封装,专门用来处理HTTP协议的数据。

  1. func sayHello(w http.ResponseWriter,r *http.Request) {
  2. fmt.Fprint(w,"HelloWorld")
  3. }
  4. func main() {
  5. http.HandleFunc("/",sayHello)
  6. err := http.ListenAndServe(":9000", nil)
  7. if err != nil {
  8. fmt.Printf("http server failed,err:%v\n",err)
  9. return
  10. }
  11. }