GET请求
func HttpGet(sourceUrl string) {
response, err := http.Get(sourceUrl)
if err != nil {
fmt.Println(err)
return
}
if response.Body != nil {
defer response.Body.Close()
}
body, err := ioutil.ReadAll(response.Body)
if response.StatusCode != 200 {
fmt.Println(err)
return
}
fmt.Println("获取内容为:", string(body))
}
POST请求
func HttpPost(sourceUrl string) {
dataStr := fmt.Sprintf("id=%v", 3)
resp, err := http.Post(sourceUrl, "application/x-www-form-urlencoded", strings.NewReader(dataStr))
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("获取内容为:", string(body))
}
有疑问加站长微信联系(非本文作者)