golang net\http包簡單的使用
阿新 • • 發佈:2019-01-02
HTTP服務端:
package main import ( "fmt" "net/http" ) func HandConn(w http.ResponseWriter, r *http.Request) { //使用者請求方法 fmt.Println(r.Method) //使用者請求地址 fmt.Println(r.URL) //請求頭 fmt.Println(r.Header) w.Write([]byte("hello go")) } func main() { //註冊處理函式,使用者連線自動呼叫指定的處理函式 http.HandleFunc("/", HandConn) //監聽http埠 http.ListenAndServe(":8000", nil) }
HTTP客戶端
package main import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" ) //httpget func httpGet() { resp, err := http.Get("http://127.0.0.1:8000") if err != nil { fmt.Println("Get err=", err) return } defer resp.Body.Close() fmt.Println("stats=", resp.Status) fmt.Println("StatusCode=", resp.StatusCode) fmt.Println("Header=", resp.Header) buf := make([]byte, 1024*4) var temp string for { n, _ := resp.Body.Read(buf) if n == 0 { break } temp += string(buf[:n]) } fmt.Println("Body=", temp) } //httppost func httpPost() { user := Users{"user1", "aaa"} if bs, err := json.Marshal(user); err == nil { req := bytes.NewBuffer([]byte(bs)) body_type := "application/json;charset=utf-8" resp, _ := http.Post("http://127.0.0.1:8000", body_type, req) body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) defer resp.Body.Close() } } type Users struct { Name string `json:"name"` ID string `json:"id"` } func main() { httpGet() httpPost() }