学以致用,今天写了一个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还没写)
本文分享了一个使用Go语言通过HTTP Post请求调用Java后端服务的具体示例。介绍了如何设置请求头、发送请求及读取响应内容,并强调了在Post请求中设置正确Content-Type的重要性。
2154

被折叠的 条评论
为什么被折叠?



