网络通信5:执行HTTP的GET/POST请求

栏目: 后端 · 前端 · 发布时间: 7年前

内容简介:版权声明:本文为博主尹成联系QQ77025077,微信18510341407原创文章,欢迎转载侵权不究。 https://blog.csdn.net/yincheng01/article/details/84136039

版权声明:本文为博主尹成联系QQ77025077,微信18510341407原创文章,欢迎转载侵权不究。 https://blog.csdn.net/yincheng01/article/details/84136039

导入依赖包

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请求

以上所述就是小编给大家介绍的《网络通信5:执行HTTP的GET/POST请求》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Trading and Exchanges

Trading and Exchanges

Larry Harris / Oxford University Press, USA / 2002-10-24 / USD 95.00

This book is about trading, the people who trade securities and contracts, the marketplaces where they trade, and the rules that govern it. Readers will learn about investors, brokers, dealers, arbit......一起来看看 《Trading and Exchanges》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器