用Go来处理GET实在是太简单了。。
GET
- u, _ := url.Parse(SMS_URL)
- q := u.Query()
- q.Set("username", SMS_USER)
- q.Set("password", SMS_PASS)
- q.Set("mobile", s.To)
- q.Set("message", s.Content)
- u.RawQuery = q.Encode()
- res,err:=http.GET(u.String());
POST分多种,一种是key/value,一种是纯body,还有。。能够上传文件的。这里先不谈上传文件(我还没有用到)
XML/HTML代码
- body := bytes.NewBuffer(msg)
- res, err := http.Post(http://api.xxx.com/, "application/x-www-form-urlencoded", body)
- v := make(url.Values)
- v.Set("email", "anything@stathat.com")
- v.Set("stat", "messages sent - female to male")
- v.Set("count", "1")
- res, err := http.PostForm("http://api.xxx.com/", values)
处理res也非常方便:
XML/HTML代码
- if err != nil {
- log.Fatal(err)
- return
- }
- result, err := ioutil.ReadAll(res.Body)
- res.Body.Close()
- if err != nil {
- log.Fatal(err)
- return
- }
- fmt.Printf("%s", result)
就是这样的方便