日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Go语言-基本的http请求操作

發布時間:2023/11/29 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Go语言-基本的http请求操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Go發起GET請求

基本的GET請求

//基本的GET請求 package mainimport ("fmt""io/ioutil""net/http" )func main() {resp, err := http.Get("http://www.hao123.com")if err != nil {fmt.Println(err)return}defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)fmt.Println(string(body))fmt.Println(resp.StatusCode)if resp.StatusCode == 200 {fmt.Println("ok")} }

帶參數的GET請求

package mainimport ("fmt""io/ioutil""net/http" )func main(){resp, err := http.Get("http://www.baidu.com?name=Paul_Chan&age=26")if err != nil {fmt.Println(err)return}defer resp.Body.Close()body, _ := ioutil.ReadAll(resp.Body)fmt.Println(string(body)) }

如果我們想要把一些參數做成變量而不是直接放到url中怎么操作,代碼例子如下:

package mainimport ("fmt""io/ioutil""net/http""net/url" )func main(){params := url.Values{}Url, err := url.Parse("http://www.baidu.com")if err != nil {return}params.Set("name","Paul_Chan")params.Set("age","26")//如果參數中有中文參數,這個方法會進行URLEncodeUrl.RawQuery = params.Encode()urlPath := Url.String()fmt.Println(urlPath) // https://www.baidu.com?age=26&name=Paul_chanresp,err := http.Get(urlPath)defer resp.Body.Close()body, _ := ioutil.ReadAll(resp.Body)fmt.Println(string(body)) }

解析JSON類型的返回結果

package mainimport ("encoding/json""fmt""io/ioutil""net/http" )type result struct {Args string `json:"args"`Headers map[string]string `json:"headers"`Origin string `json:"origin"`Url string `json:"url"` }func main() {resp, err := http.Get("http://xxx.com")if err != nil {return}defer resp.Body.Close()body, _ := ioutil.ReadAll(resp.Body)fmt.Println(string(body))var res result_ = json.Unmarshal(body,&res)fmt.Printf("%#v", res) }

GET請求添加請求頭

package mainimport ("fmt""io/ioutil""net/http" )func main() {client := &http.Client{}req,_ := http.NewRequest("GET","http://xxx.com",nil)req.Header.Add("name","Paul_Chan")req.Header.Add("age","26")resp,_ := client.Do(req)body, _ := ioutil.ReadAll(resp.Body)fmt.Printf(string(body)) }

從上述的結果可以看出我們設置的頭是成功了:

{"args": {}, "headers": {"Accept-Encoding": "gzip", "Age": "26", "Host": "xxx.com", "Name": "Paul_Chan", "User-Agent": "Go-http-client/1.1"}, "origin": "211.138.20.170, 211.138.20.170", "url": "https://xxx.com" }

golang 發起POST請求

基本的POST使用

package mainimport ("fmt""io/ioutil""net/http""net/url" )func main() {urlValues := url.Values{}urlValues.Add("name","Paul_Chan")urlValues.Add("age","26")resp, _ := http.PostForm("http://xxx.com/post",urlValues)body, _ := ioutil.ReadAll(resp.Body)fmt.Println(string(body)) } //結果如下: /****************** {"args": {}, "data": "", "files": {}, "form": {"age": "26", "name": "Paul_Chan"}, "headers": {"Accept-Encoding": "gzip", "Content-Length": "19", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Go-http-client/1.1"}, "json": null, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/post" } ******************/

另外一種方式

package mainimport ("fmt""io/ioutil""net/http""net/url""strings" )func main() {urlValues := url.Values{"name":{"Paul_Chan"},"age":{"26"},}reqBody:= urlValues.Encode()resp, _ := http.Post("http://xxx.com/post", "text/html",strings.NewReader(reqBody))body,_:= ioutil.ReadAll(resp.Body)fmt.Println(string(body)) } //結果如下: /************** {"args": {}, "data": "age=26&name=Paul_Chan", "files": {}, "form": {}, "headers": {"Accept-Encoding": "gzip", "Content-Length": "19", "Content-Type": "text/html", "Host": "httpbin.org", "User-Agent": "Go-http-client/1.1"}, "json": null, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/post" } ***************/

發送JSON數據的post請求

package mainimport ("bytes""encoding/json""fmt""io/ioutil""net/http" )func main() {client := &http.Client{}data := make(map[string]interface{})data["name"] = "zhaofan"data["age"] = "23"bytesData, _ := json.Marshal(data)req, _ := http.NewRequest("POST","http://httpbin.org/post",bytes.NewReader(bytesData))resp, _ := client.Do(req)body, _ := ioutil.ReadAll(resp.Body)fmt.Println(string(body)) } //結果如下: /************* {"args": {}, "data": "{\"age\":\"23\",\"name\":\"zhaofan\"}", "files": {}, "form": {}, "headers": {"Accept-Encoding": "gzip", "Content-Length": "29", "Host": "httpbin.org", "User-Agent": "Go-http-client/1.1"}, "json": {"age": "23", "name": "zhaofan"}, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/post" } *************/

不用client的post請求

package mainimport ("bytes""encoding/json""fmt""io/ioutil""net/http" )func main() {data := make(map[string]interface{})data["name"] = "zhaofan"data["age"] = "23"bytesData, _ := json.Marshal(data)resp, _ := http.Post("http://httpbin.org/post","application/json", bytes.NewReader(bytesData))body, _ := ioutil.ReadAll(resp.Body)fmt.Println(string(body)) }

做任何事情,都要以創業的心態去干!

轉載于:https://www.cnblogs.com/Paul-watermelon/p/11386392.html

總結

以上是生活随笔為你收集整理的Go语言-基本的http请求操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。