golang 发送Get 与 POST请求
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func main() {
params := url.Values{} //参数集合
reqUrl,err := url.Parse("http://zlbb.ankld.cn/ex.php")//请求地址
if err != nil {
panic(err.Error())
}
params.Set("age","10") //设置参数
params.Set("id","100")
reqUrl.RawQuery = params.Encode() //组合url
resp, err := http.Get(reqUrl.String())//发起get请求
if err != nil {
panic(err.Error())
}
defer resp.Body.Close() //关闭请求
body , err := ioutil.ReadAll(resp.Body)//解析请求信息
if err != nil {
panic(err.Error())
}
var results map[string]interface{} //请求结果集
err = json.Unmarshal(body,&results) //转换为map
if err != nil {
panic(err.Error())
}
fmt.Println(results) //输出结果集
}