追风赶月莫停留,平芜尽处是春山。这篇文章主要讲述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]
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\\n", err)
return
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil
fmt.Printf("read from resp.Body failed, err:%v\\n", err)
return
fmt.Print(string(body))
带参数:
参数需要使用到net/url来处理
func main()
apiUrl := "http://127.0.0.1:9090/get"
// URL param
data := url.Values
data.Set("name", "mi")
data.Set("age", "18")
u, err := url.ParseRequestURI(apiUrl)
if err != nil
fmt.Printf("parse url requestUrl failed, err:%v\\n", 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\\n", err)
return
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil
fmt.Printf("get resp failed, err:%v\\n", err)
return
fmt.Println(string(b))
带header头部信息:
import (
"net/http"
)
url := "https://b959e645-00ae-4bc3-8a55-7224d08b1d91.mock.pstmn.io/user/1"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "xxxx")
response, err := http.DefaultClient.Do(req)
对应server端:
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]
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=mi& age=18"
// json
contentType := "application/json"
data := `"name":"mi","age":18`
resp, err := http.Post(url, contentType, strings.NewReader(data))
if err != nil
fmt.Printf("post failed, err:%v\\n", err)
return
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil
fmt.Printf("get resp failed, err:%v\\n", err)
return
fmt.Println(string(b))
PostForm方式
func httpPostForm()
resp, err := http.PostForm("http://www.01happy.com/demo/accept.php",
url.Values"key": "Value", "id": "123")
if err != nil
// handle error
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil
// handle error
fmt.Println(string(body))
json:
func testPostJson()
data := make(map[string]interface)
data["site"] = "www.360.com"
data["name"] = "xxx"
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))
带有headers参数
func httpDo()
client := & http.Client
req, err := http.NewRequest("POST", "http://www.01happy.com/demo/accept.php", strings.NewReader("name=cjb"))
if err != nil
// handle error
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Cookie", "name=anny")
resp, err := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil
// handle error
fmt.Println(string(body))
对应server端:
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\\n", 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.ConfigRootCAs: pool,
DisableCompression: true,
client := & http.ClientTransport: 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协议的数据。具体的代码如下:package main
import (
"fmt"
"net/http"
)
func sayHello(w http.ResponseWriter, r *http.Request)
fmt.Fprintln(w, "Hello shanghai!")
func main()
http.HandleFunc("/", sayHello)
err := http.ListenAndServe(":9090", nil)
if err != nil
fmt.Printf("http server failed, err:%v\\n", 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())
demo:
package main
import (
"encoding/json"
_"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
var (
api = "http://xxxxx.com"
token = "9adf92861"
)
type Result struct
Code int `json:"code"`
Msg string `json:"msg"`
type Resp struct
Code string `json:"code"`
Msg string `json:"msg"`
func wechatUrlHandle(w http.ResponseWriter, r *http.Request)
// 获取链接地址
var wechatUrl string
values := r.URL.Query()
wechatUrl = values.Get("url")
// 验证URL的正确性
targetUrl, err := url.ParseRequestURI(wechatUrl)
if err != nil
fmt.Printf("parse url failed, err:%v\\n", err)
fmt.Printf(fmt.Sprintf("url: %v", targetUrl))
// 拼接参数发起请求
data := fmt.Sprintf("token=%s& url=%s", token, targetUrl.String())
headers := "application/x-www-form-urlencoded"
res, err := http.Post(api, headers, strings.NewReader(data))
if err != nil
fmt.Printf("post failed, err: %v\\n", err)
return
// 关闭请求资源
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil
fmt.Fprintf(w, fmt.Sprintf("get resp failed. err: %v\\n", err))
return
var info Result
var ress Resp
w.Header().Set("Content-Type", "application/json")
if err := json.Unmarshal(body, & info); err == nil
fmt.Println(info.Code, info.Msg)
ress.Code = "200"
ress.Msg = "Success"
t, _ := json.Marshal(ress)
w.Write(t)
else
ress.Code = "200"
ress.Msg = "failure"
t, _ := json.Marshal(ress)
fmt.Println("err: ", err)
w.Write(t)
func main()
http.HandleFunc("/wechat/url", wechatUrlHandle)
err := http.ListenAndServe(":9090", nil)// 处理器参数一般未nil
if err != nil
fmt.Printf("Http server failed. err: %v\\n", err)
return
【Go基础库之net/http】
推荐阅读
- 高性能消息队列之nsq
- Django之信号
- linux常用命令安装
- go官方包依赖管理工具之mod
- Linux虚拟机的安装问题
- Kafka之入门
- #导入Word文档图片# Linux系统与终端基础命令介绍
- Linux虚拟机环境搭建JAVA安装flink安装
- 如何在 JavaScript 中让代码更加精简