Go语言基础包之net/http
Go语言内置的net/http
包十分的优秀,提供了HTTP客户端和服务端的实现。
net/http介绍
Go语言内置的net/http
包提供了HTTP客户端和服务端的实现。
HTTP协议
超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。
HTTP客户端
基本的HTTP/HTTPS请求
Get、Head、Post和PostForm函数发出HTTP/HTTPS请求。
resp, err := http.Get("http://example.com/")
...
resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf)
...
resp, err := http.PostForm("http://example.com/form",
url.Values{"key": {"Value"}, "id": {"123"}})
程序在使用完response后必须关闭回复的主体。
resp, err := http.Get("http://example.com/")
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// ...
GET请求示例
使用net/http
包编写一个简单的发送HTTP请求的Client端,代码如下:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
resp, err := http.Get("https://www.liwenzhou.com/")
if err != nil {
fmt.Printf("get failed, err:%v
", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("read from resp.Body failed, err:%v
", err)
return
}
fmt.Print(string(body))
}
将上面的代码保存之后编译成可执行文件,执行之后就能在终端打印liwenzhou.com
网站首页的内容了,我们的浏览器其实就是一个发送和接收HTTP协议数据的客户端,我们平时通过浏览器访问网页其实就是从网站的服务器接收HTTP数据,然后浏览器会按照HTML、CSS等规则将网页渲染展示出来。
带参数的GET请求示例
关于GET请求的参数需要使用Go语言内置的net/url
这个标准库来处理。
func main() {
apiUrl := "http://127.0.0.1:9090/get"
// URL param
data := url.Values{}
data.Set("name", "小王子")
data.Set("age", "18")
u, err := url.ParseRequestURI(apiUrl)
if err != nil {
fmt.Printf("parse url requestUrl failed, err:%v
", err)
}
u.RawQuery = data.Encode() // URL encode
fmt.Println(u.String())
resp, err := http.Get(u.String())
if err != nil {
fmt.Printf("post failed, err:%v
", err)
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("get resp failed, err:%v
", err)
return
}
fmt.Println(string(b))
}
对应的Server端HandlerFunc如下:
func getHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
data := r.URL.Query()
fmt.Println(data.Get("name"))
fmt.Println(data.Get("age"))
answer := `{"status": "ok"}`
w.Write([]byte(answer))
}
Post请求示例
上面演示了使用net/http
包发送GET
请求的示例,发送POST
请求的示例代码如下:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// net/http post demo
func main() {
url := "http://127.0.0.1:9090/post"
// 表单数据
//contentType := "application/x-www-form-urlencoded"
//data := "name=小王子&age=18"
// json
contentType := "application/json"
data := `{"name":"小王子","age":18}`
resp, err := http.Post(url, contentType, strings.NewReader(data))
if err != nil {
fmt.Printf("post failed, err:%v
", err)
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("get resp failed, err:%v
", err)
return
}
fmt.Println(string(b))
}
对应的Server端HandlerFunc如下:
func postHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
// 1. 请求类型是application/x-www-form-urlencoded时解析form数据
r.ParseForm()
fmt.Println(r.PostForm) // 打印form数据
fmt.Println(r.PostForm.Get("name"), r.PostForm.Get("age"))
// 2. 请求类型是application/json时从r.Body读取数据
b, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Printf("read request.Body failed, err:%v
", err)
return
}
fmt.Println(string(b))
answer := `{"status": "ok"}`
w.Write([]byte(answer))
}
自定义Client
要管理HTTP客户端的头域、重定向策略和其他设置,创建一个Client:
client := &http.Client{
CheckRedirect: redirectPolicyFunc,
}
resp, err := client.Get("http://example.com")
// ...
req, err := http.NewRequest("GET", "http://example.com", nil)
// ...
req.Header.Add("If-None-Match", `W/"wyzzy"`)
resp, err := client.Do(req)
// ...
自定义Transport
要管理代理、TLS配置、keep-alive、压缩和其他设置,创建一个Transport:
tr := &http.Transport{
TLSClientConfig: &tls.Config{RootCAs: pool},
DisableCompression: true,
}
client := &http.Client{Transport: tr}
resp, err := client.Get("https://example.com")
Client和Transport类型都可以安全的被多个goroutine同时使用。出于效率考虑,应该一次建立、尽量重用。
服务端
默认的Server
ListenAndServe使用指定的监听地址和处理器启动一个HTTP服务端。处理器参数通常是nil,这表示采用包变量DefaultServeMux作为处理器。
Handle和HandleFunc函数可以向DefaultServeMux添加处理器。
http.Handle("/foo", fooHandler)
http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
log.Fatal(http.ListenAndServe(":8080", nil))
默认的Server示例
使用Go语言中的net/http
包来编写一个简单的接收HTTP请求的Server端示例,net/http
包是对net包的进一步封装,专门用来处理HTTP协议的数据。具体的代码如下:
// http server
func sayHello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello 沙河!")
}
func main() {
http.HandleFunc("/", sayHello)
err := http.ListenAndServe(":9090", nil)
if err != nil {
fmt.Printf("http server failed, err:%v
", err)
return
}
}
将上面的代码编译之后执行,打开你电脑上的浏览器在地址栏输入127.0.0.1:9090
回车,此时就能够看到如下页面了。
自定义Server
要管理服务端的行为,可以创建一个自定义的Server:
s := &http.Server{
Addr: ":8080",
Handler: myHandler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Fatal(s.ListenAndServe())
之前用python写各种网络请求的时候写的非常顺手,但是当打算用golang写的时候才发现相对来说还是python的那种方式用的更加顺手,习惯golang的用法之后也就差别不大了,下面主要整理了常用的通过golang发起的GET请求以及POST请求的代码例子
golang发起GET请求
基本的GET请求
//基本的GET请求 package main import ( "fmt" "io/ioutil" "net/http" ) func main() { resp, err := http.Get("http://httpbin.org/get") if err != nil { fmt.Println(err) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) fmt.Println(resp.StatusCode) if resp.StatusCode == 200 { fmt.Println("ok") } }
带参数的Get请求
package main import ( "fmt" "io/ioutil" "net/http" ) func main(){ resp, err := http.Get("http://httpbin.org/get?name=zhaofan&age=23") if err != nil { fmt.Println(err) return } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
但是如果我们想要把一些参数做成变量而不是直接放到url中怎么操作,代码例子如下:
package main import ( "fmt" "io/ioutil" "net/http" "net/url" ) func main(){ params := url.Values{} Url, err := url.Parse("http://httpbin.org/get") if err != nil { return } params.Set("name","zhaofan") params.Set("age","23") //如果参数中有中文参数,这个方法会进行URLEncode Url.RawQuery = params.Encode() urlPath := Url.String() fmt.Println(urlPath) // https://httpbin.org/get?age=23&name=zhaofan resp,err := http.Get(urlPath) defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
解析JSON类型的返回结果
package main import ( "encoding/json" "fmt" "io/ioutil" "net/http" ) type result struct { Args string `json:"args"` Headers map[string]string `json:"headers"` Origin string `json:"origin"` Url string `json:"url"` } func main() { resp, err := http.Get("http://httpbin.org/get") if err != nil { return } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) var res result _ = json.Unmarshal(body,&res) fmt.Printf("%#v", res) }
GET请求添加请求头
package main import ( "fmt" "io/ioutil" "net/http" ) func main() { client := &http.Client{} req,_ := http.NewRequest("GET","http://httpbin.org/get",nil) req.Header.Add("name","zhaofan") req.Header.Add("age","3") resp,_ := client.Do(req) body, _ := ioutil.ReadAll(resp.Body) fmt.Printf(string(body)) }
从上述的结果可以看出我们设置的头是成功了:
{ "args": {}, "headers": { "Accept-Encoding": "gzip", "Age": "3", "Host": "httpbin.org", "Name": "zhaofan", "User-Agent": "Go-http-client/1.1" }, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/get" }
golang 发起POST请求
基本的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://httpbin.org/post",urlValues) body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
结果如下:
{ "args": {}, "data": "", "files": {}, "form": { "age": "22", "name": "zhaofan" }, "headers": { "Accept-Encoding": "gzip", "Content-Length": "19", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Go-http-client/1.1" }, "json": null, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/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://httpbin.org/post", "text/html",strings.NewReader(reqBody)) body,_:= ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
结果如下:
{ "args": {}, "data": "age=23&name=zhaofan", "files": {}, "form": {}, "headers": { "Accept-Encoding": "gzip", "Content-Length": "19", "Content-Type": "text/html", "Host": "httpbin.org", "User-Agent": "Go-http-client/1.1" }, "json": null, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/post" }
发送JSON数据的post请求
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://httpbin.org/post",bytes.NewReader(bytesData)) resp, _ := client.Do(req) body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
结果如下:
{ "args": {}, "data": "{"age":"23","name":"zhaofan"}", "files": {}, "form": {}, "headers": { "Accept-Encoding": "gzip", "Content-Length": "29", "Host": "httpbin.org", "User-Agent": "Go-http-client/1.1" }, "json": { "age": "23", "name": "zhaofan" }, "origin": "211.138.20.170, 211.138.20.170", "url": "https://httpbin.org/post" }
不用client的post请求
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://httpbin.org/post","application/json", bytes.NewReader(bytesData)) body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }