导入依赖包

import (
	"fmt"
	"net/http"
	"io/ioutil"
	"strings"
)

提交GET请求并获得返回

func main521() {
	url := "http://www.baidu.com/s?wd=肉"

	resp, err := http.Get(url)
	if err != nil {
		fmt.Println("错误")
	}
	defer resp.Body.Close()

	bodyBytes, _ := ioutil.ReadAll(resp.Body) //读取信息
	fmt.Println(string(bodyBytes))            //读取网页源代码
}

提交POST请求并获得返回

func main522() {
	//url := "http://www.baidu.com"
	url := "https://httpbin.org/post?name=张三"

	resp, err := http.Post(
		url,
		"application/x-www-form-urlencoded",
		strings.NewReader("id=nimei"))
	if err != nil {
		fmt.Println("错误")
	}
	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body) //读取信息
	fmt.Println(string(body))            //读取网页源代码

}

学院Go语言视频主页
https://edu.csdn.net/lecturer/1928

[清华团队带你实战区块链开发]
(https://ke.qq.com/course/344443?tuin=3d17195d)
扫码获取海量视频及源码 QQ群:721929980
网络通信5:执行HTTP的GET/POST请求

相关文章:

  • 2021-10-28
  • 2021-11-18
  • 2021-12-16
  • 2021-08-14
  • 2021-06-03
  • 2022-01-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-25
  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2021-07-29
相关资源
相似解决方案