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

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

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

查看所有标签

猜你喜欢:

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

Basics of Web Design

Basics of Web Design

Terry Felke-Morris / Addison-Wesley / 2013-1-28 / USD 98.40

Basics of Web Design: HTML5 and CSS3, 2e covers the basic concepts that web designers need to develop their skills: * Introductory Internet and Web concepts* Creating web pages with HTML5* Configurin......一起来看看 《Basics of Web Design》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

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

Base64 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具