import ( "net/http" "net/url" ) //发送一个简单的get请求 func GetRequest { //联系使用 make(map[string]string) queryData := make(map[string]string) //创建空间, queryData["params"] = c.QueryParam("params") u, _ := url.Parse("http://baidu.com/api/member/getUserSafeDevic") q := u.Query() q.Set("params", queryData["params"]) u.RawQuery = q.Encode() resp, _ := http.Get(u.String()) //开启一个Get请求注意Get中的参数是 String result, _ := ioutil.ReadAll(resp.Body) //将接口返回的body数据给result resp.Body.Close() //关闭请求 fmt.Printf("%s", resp) //打印结果 }
//通过http.Client 发送post请求 func PostUserSafeDevice { q := url.Values{} q.Set("mac", c.QueryParam("mac")) q.Set("imei", c.QueryParam("imei")) body := ioutil.NopCloser(strings.NewReader(q.Encode())) client := &http.Client{} req, _ := http.NewRequest("POST", "http://baidu.com/postDevice", body) req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value") resp, _ := client.Do(req) defer resp.Body.Close() result, _ := ioutil.ReadAll(resp.Body) fmt.Printf("%s", result) }
时间: 2024-09-30 06:11:08