go 网络请求篇
---恢復(fù)內(nèi)容開始---
今天特意找了下go的網(wǎng)絡(luò)請(qǐng)求篇,get請(qǐng)求是ok的,post請(qǐng)求一直不下來,搜索了下,代碼都差不多,無法拿到post數(shù)據(jù),先整理一篇,親測(cè)可用。
針對(duì)post ,先來說下post 四種提交數(shù)據(jù)方式(Content-Type) ?鏈接
- application/x-www-form-urlencoded ? ???key1=val1&key2=val2 ? ? ??PHP 中,$_POST['title'] 可以獲取到 title 的值,$_POST['sub'] 可以得到 sub 數(shù)組。
- multipart/form-data ? ? 文件上傳
- application/json ? ?在請(qǐng)求頭中 Content-Type 為 application/json 時(shí),從 php://input 里獲得原始輸入流
?
- text/xml ? ? ? 不熟悉
?
? 我當(dāng)時(shí)測(cè)試的為php接口,然后content-type 寫成了 application/json,然后怎么post都是拿到的域名的html頁面代碼。以下為測(cè)試通過代碼
?
1、get請(qǐng)求
1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "log" 7 "net/http" 8 "reflect" 9 ) 10 11 func main() { 12 13 url := "http://api.budejie.com/api/api_open.php?a=list&appname=baisibudejie_hd&asid=C1180CB8-F460-4385-A77C-97CD1AA83DFD&c=data&client=ipad&device=ios%20%E8%AE%BE%E5%A4%87&from=ios&jbk=0&mac=02:00:00:00:00:00&market=&maxtime=&openudid=78336166d6a434b4cf1634410eb3b692d6d3a4ee&order=ctime&page=1&per=20000&systemversion=7.1&type=10&ver=2.0.3" 14 15 resp, err := http.Get(url) 16 17 if err != nil { 18 // 錯(cuò)誤處理 19 log.Println(err) 20 return 21 } 22 23 defer resp.Body.Close() //關(guān)閉鏈接 24 25 fmt.Printf("resp status %s,statusCode %d\n", resp.Status, resp.StatusCode) 26 27 fmt.Printf("resp Proto %s\n", resp.Proto) 28 29 fmt.Printf("resp content length %d\n", resp.ContentLength) 30 31 fmt.Printf("resp transfer encoding %v\n", resp.TransferEncoding) 32 33 fmt.Printf("resp Uncompressed %t\n", resp.Uncompressed) 34 35 fmt.Println(reflect.TypeOf(resp.Body)) 36 buf := bytes.NewBuffer(make([]byte, 0, 512)) 37 length, _ := buf.ReadFrom(resp.Body) 38 fmt.Println(len(buf.Bytes())) 39 fmt.Println(length) 40 fmt.Println(string(buf.Bytes())) 41 }
2、post請(qǐng)求
package mainimport ("bytes""fmt""io/ioutil""net/http""net/url""time"
)func main() {url := "http://api.budejie.com/api/api_open.php"var data map[string]string /*創(chuàng)建集合 */data = make(map[string]string)data["a"] = "list"data["appname"] = "baisibudejie_hd"data["asid"] = "C1180CB8-F460-4385-A77C-97CD1AA83DFD"data["c"] = "data"data["client"] = "ipad"data["device"] = "ios"data["from"] = "ios"data["jbk"] = "0"data["mac"] = "02:00:00:00:00:00"data["openudid"] = "78336166d6a434b4cf1634410eb3b692d6d3a4ee"data["order"] = "ctime"data["page"] = "1"data["per"] = "20"data["systemversion"] = "7.1"data["type"] = "10"data["ver"] = "2.0.3"data["market"] = ""data["maxtime"] = ""contentType := "application/x-www-form-urlencoded"json := Post(url, data, contentType)fmt.Println(json)}//發(fā)送POST請(qǐng)求
//url:請(qǐng)求地址,data:POST請(qǐng)求提交的數(shù)據(jù),contentType:請(qǐng)求體格式,如:application/json
//content:請(qǐng)求放回的內(nèi)容
func Post(urlStr string, data map[string]string, contentType string) (content string) {postValues := url.Values{}for postKey, PostValue := range data {postValues.Set(postKey, PostValue)}postDataStr := postValues.Encode()postDataBytes := []byte(postDataStr)postBytesReader := bytes.NewReader(postDataBytes)req, err := http.NewRequest("POST", urlStr, postBytesReader)req.Header.Add("content-type", contentType)if err != nil {panic(err)}defer req.Body.Close()client := &http.Client{Timeout: 5 * time.Second}resp, error := client.Do(req)if error != nil {panic(error)}defer resp.Body.Close()result, _ := ioutil.ReadAll(resp.Body)content = string(result)return
}
?
?
?
---恢復(fù)內(nèi)容結(jié)束---
轉(zhuǎn)載于:https://www.cnblogs.com/lpwlpw/p/9967093.html
總結(jié)