go 網路請求篇
阿新 • • 發佈:2018-11-16
---恢復內容開始---
今天特意找了下go的網路請求篇,get請求是ok的,post請求一直不下來,搜尋了下,程式碼都差不多,無法拿到post資料,先整理一篇,親測可用。
針對post ,先來說下post 四種提交資料方式(Content-Type) 連結
- application/x-www-form-urlencoded key1=val1&key2=val2 PHP 中,$_POST['title'] 可以獲取到 title 的值,$_POST['sub'] 可以得到 sub 陣列。
- multipart/form-data 檔案上傳
- application/json 在請求頭中 Content-Type 為 application/json 時,從 php://input 裡獲得原始輸入流
- text/xml 不熟悉
我當時測試的為php介面,然後content-type 寫成了 application/json,然後怎麼post都是拿到的域名的html頁面程式碼。以下為測試通過程式碼
1、get請求
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 // 錯誤處理 19 log.Println(err) 20 return 21 } 22 23 defer resp.Body.Close() //關閉連結 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請求
package main import ( "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 /*建立集合 */ 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) } //傳送POST請求 //url:請求地址,data:POST請求提交的資料,contentType:請求體格式,如:application/json //content:請求放回的內容 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 }
---恢復內容結束---