golang http 实例

前端之家收集整理的这篇文章主要介绍了golang http 实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main import ( "fmt" "io/IoUtil" "net/http" "net/url" "strings" ) func httpGet() { resp,err := http.Get("http://www.01happy.com/demo/accept.PHP?id=1") if err != nil { // handle error } defer resp.Body.Close() body,err := IoUtil.ReadAll(resp.Body) if err != nil { // handle error } fmt.Println(string(body)) } func httpPost() { resp,err := http.Post("http://www.01happy.com/demo/accept.PHP","application/x-www-form-urlencoded",strings.NewReader("name=cjb")) if err != nil { fmt.Println(err) } defer resp.Body.Close() body,err := IoUtil.ReadAll(resp.Body) if err != nil { // handle error } fmt.Println(string(body)) } func httpPostForm() { resp,err := http.PostForm("http://www.01happy.com/demo/accept.PHP",url.Values{"key": {"Value"},"id": {"123"}}) if err != nil { // handle error } defer resp.Body.Close() body,err := IoUtil.ReadAll(resp.Body) if err != nil { // handle error } fmt.Println(string(body)) } func httpDo() { client := &http.Client{} req,err := http.NewRequest("POST","http://www.01happy.com/demo/accept.PHP",strings.NewReader("name=cjb")) if err != nil { // handle error } req.Header.Set("Content-Type","application/x-www-form-urlencoded") req.Header.Set("Cookie","name=anny") resp,err := client.Do(req) defer resp.Body.Close() body,err := IoUtil.ReadAll(resp.Body) if err != nil { // handle error } fmt.Println(string(body)) }

猜你在找的Go相关文章