---恢复内容开始---
今天特意找了下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 ", resp.Status, resp.StatusCode) 26 27 fmt.Printf("resp Proto %s ", resp.Proto) 28 29 fmt.Printf("resp content length %d ", resp.ContentLength) 30 31 fmt.Printf("resp transfer encoding %v ", resp.TransferEncoding) 32 33 fmt.Printf("resp Uncompressed %t ", 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 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 /*创建集合 */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 }
---恢复内容结束---