go 发送http请求
2023-09-14 09:12:08 时间
普通的get请求
package main import ( "io/ioutil" "fmt" "net/http" ) func main() { res,_ :=http.Get("https://www.baidu.com/") defer res.Body.Close() body,_ := ioutil.ReadAll(res.Body) fmt.Print(body) }
带参数的get请求(参数不放在url里)
package main import ( "fmt" "io/ioutil" "net/http" "net/url" ) func main(){ params := url.Values{} Url, _:= url.Parse("https://www.baidu.com/") params.Set("name","zhaofan") params.Set("age","23") //如果参数中有中文参数,这个方法会进行URLEncode Url.RawQuery = params.Encode() urlPath := Url.String() fmt.Println(urlPath) //等同于https://www.xxx.com?age=23&name=zhaofan resp,_ := http.Get(urlPath) defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
get请求添加请求头
package main import ( "fmt" "io/ioutil" "net/http" ) func main() { client := &http.Client{} req,_ := http.NewRequest("GET","http://www.xxx.com",nil) req.Header.Add("name","zhaofan") req.Header.Add("age","3") resp,_ := client.Do(req)
defer resp.Body.close() body, _ := ioutil.ReadAll(resp.Body) fmt.Printf(string(body)) }
post请求
package main import ( "fmt" "io/ioutil" "net/http" "net/url" ) func main() { urlValues := url.Values{} urlValues.Add("name","zhaofan") urlValues.Add("age","22") resp, _ := http.PostForm("http://www.xxx.com",urlValues)
defer resp.Body.close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
post请求的另一种方式
package main import ( "fmt" "io/ioutil" "net/http" "net/url" "strings" ) func main() { urlValues := url.Values{ "name":{"zhaofan"}, "age":{"23"}, } reqBody:= urlValues.Encode() resp, _ := http.Post("http://www.xxx.com/post", "text/html",strings.NewReader(reqBody))
defer resp.Body.close() body,_:= ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
post请求发送json数据
package main import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" ) func main() { client := &http.Client{} data := make(map[string]interface{}) data["name"] = "zhaofan" data["age"] = "23" bytesData, _ := json.Marshal(data) req, _ := http.NewRequest("POST","http://www.xxx.com",bytes.NewReader(bytesData)) resp, _ := client.Do(req)
defer resp.Body.close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
不用client
package main import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" ) func main() { data := make(map[string]interface{}) data["name"] = "zhaofan" data["age"] = "23" bytesData, _ := json.Marshal(data) resp, _ := http.Post("http://www.xxx.com","application/json", bytes.NewReader(bytesData))
defer resp.Body.close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
说白了,我们记住http.get 和http.post就可以了
相关文章
- 22.Go语言-反射
- 使用 http-proxy 代理 HTTP 请求时遇到的 the requested url is invalid 错误消息
- Go-Excelize API源码阅读(十一)—— GetActiveSheetIndex()
- http前缀树路由算法和Go源码分析
- 深入了解 Go ELF 信息
- 「Go工具箱」go语言csrf库的使用方式和实现原理
- GO语言开篇-Go语言急速入门(基础知识点)| 青训营笔记
- 【Flutter】HTTP 网络操作 ( 引入 http 插件 | 测试网站 | Get 请求 | Post 请求 | 将响应结果转为 Dart 对象 | Future 异步调用 )
- Go-标准库-encoding(一)
- Go-包管理-go get(一)
- Go-包管理-go mod(二)
- Go-包管理-go clean
- 哪些项目使用Go语言开发?
- Go语言宕机恢复(recover)——防止程序崩溃
- Go基础之锁的初识详解编程语言
- 请求使用Linux发送HTTP请求的方法(linux发送http)
- Go语言-基本的http请求操作详解编程语言
- Go_ go mod 命令解决墙的问题详解编程语言
- Go语言网络爬虫的接口设计
- 精通Go语言开发Redis应用(go语言redis开发)
- 程序Linux上运行Go语言程序的指南(linux运行go)
- 如何在Linux上安装Go语言?——简单易懂的指南(linux安装go语言)
- Linux下快速访问HTTP(linux 访问http)
- 给Oracle数据库添加专业技能,Go大发展(go_db_oracle)
- Oracle Go用法快速指南(oracle go用法)