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

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

内容简介:版权声明:本文为博主尹成联系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请求》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Sass and Compass in Action

Sass and Compass in Action

Wynn Netherland、Nathan Weizenbaum、Chris Eppstein、Brandon Mathis / Manning Publications / 2013-8-2 / USD 44.99

Written by Sass and Compass creators * Complete Sass language reference * Covers prominent Compass community plug-ins * Innovative approach to creating stylesheets Cascading Style Sheets paint the we......一起来看看 《Sass and Compass in Action》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具