学以致用,今天写了一个go http的程序,来调用我之前写的java方法。
package main
import (
"fmt"
"io/ioutil"
"net/http"
//"strings"
)
const queryCustomerUrl = "http://172.31.3.64:8086/app/cm/v2/custlabel/queryLabel.do?id=1"
func getCustomer(){
//resp,err := http.Post(queryCustomerUrl,"application/x-www-form-urlencode",strings.NewReader(""))
resp,err := http.Post(queryCustomerUrl,"application/x-www-form-urlencode",nil)
if(err != nil){
fmt.Println(err)
}
body, err := ioutil.ReadAll(resp.Body)
if(err != nil){
fmt.Println(err)
}
fmt.Println(string(body))
defer resp.Body.Close()
}
func main(){
getCustomer()
}
注意,如果是用post请求,Post()方法的第二个参数必须设置为 “application/x-www-form-urlencode”,其中被注释的那行也是可以的,直接传nil也是可以的
上面的例子只有一个参数,所以我直接拼在URL后面了,如果参数多,建议使用json来传参(暂时demo还没写)