golang http标准库
http标准库了http客户端和服务器的实现,注意了,客户端实现可以发出http请求,并解析响应。服务器可以实现http server功能。市面上的所有golang web框架都是基于http标准库实现的。
http标准库客户端功能
发出GET请求
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
func testGet() {
// https://www.juhe.cn/box/index/id/73
url := "http://apis.juhe.cn/simpleWeather/query?key=087d7d10f700d20e27bb753cd806e40b&city=北京"
r, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
fmt.Printf("b: %v\n", string(b))
}
GET请求,把一些参数做成变量而不是直接放到url
func testGet2() {
params := url.Values{}
Url, err := url.Parse("http://apis.juhe.cn/simpleWeather/query")
if err != nil {
return
}
params.Set("key", "087d7d10f700d20e27bb753cd806e40b")
params.Set("city", "北京")
//如果参数中有中文参数,这个方法会进行URLEncode
Url.RawQuery = params.Encode()
urlPath := Url.String()
fmt.Println(urlPath)
resp, err := http.Get(urlPath)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
解析JSON类型的返回结果
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
func testParseJson() {
type result struct {
Args string `json:"args"`
Headers map[string]string `json:"headers"`
Origin string `json:"origin"`
Url string `json:"url"`
}
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)
}
运行结果
{
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Host": "httpbin.org",
"User-Agent": "Go-http-client/1.1",
"X-Amzn-Trace-Id": "Root=1-61b16029-731c99ba4591c9bd3db53edd"
},
"origin": "115.171.25.28",
"url": "http://httpbin.org/get"
}
main.result{Args:"", Headers:map[string]string{"Accept-Encoding":"gzip", "Host":"httpbin.org", "User-Agent":"Go-http-client/1.1", "X-Amzn-Trace-Id":"Root=1-61b16029-731c99ba4591c9bd3db53edd"}, Origin:"115.171.25.28", Url:"http://httpbin.org/get"}
GET请求添加请求头
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
func testAddHeader() {
client := &http.Client{}
req, _ := http.NewRequest("GET", "http://httpbin.org/get", nil)
req.Header.Add("name", "zs")
req.Header.Add("age", "80")
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",
"X-Amzn-Trace-Id": "Root=1-61b16107-5814e133649862c20ab1c26f"
},
"origin": "115.171.25.28",
"url": "http://httpbin.org/get"
}
发出POST请求
func testPost() {
path := "http://apis.juhe.cn/simpleWeather/query"
urlValues := url.Values{}
urlValues.Add("key", "087d7d10f700d20e27bb753cd806e40b")
urlValues.Add("city", "北京")
r, err := http.PostForm(path, urlValues)
if err != nil {
log.Fatal(err)
}
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
fmt.Printf("b: %v\n", string(b))
}
另外一种方式
func testPost2() {
urlValues := url.Values{
"name": {"zs"},
"age": {"80"},
}
reqBody := urlValues.Encode()
resp, _ := http.Post("http://httpbin.org/post", "text/html", strings.NewReader(reqBody))
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
发送JSON数据的post请求
func testPostJson() {
data := make(map[string]interface{})
data["site"] = "www.duoke360.com"
data["name"] = "多课网"
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))
}
使用Client自定义请求
func testClient() {
client := http.Client{
Timeout: time.Second * 5,
}
url := "http://apis.juhe.cn/simpleWeather/query?key=087d7d10f700d20e27bb753cd806e40b&city=北京"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("referer", "http://apis.juhe.cn/")
res, err2 := client.Do(req)
if err2 != nil {
log.Fatal(err2)
}
defer res.Body.Close()
b, _ := ioutil.ReadAll(res.Body)
fmt.Printf("b: %v\n", string(b))
}
HTTP Server
使用golang实现一个http server非常简单,代码如下:
func testHttpServer() {
// 请求处理函数
f := func(resp http.ResponseWriter, req *http.Request) {
io.WriteString(resp, "hello world")
}
// 响应路径,注意前面要有斜杠 /
http.HandleFunc("/hello", f)
// 设置监听端口,并监听,注意前面要有冒号:
err := http.ListenAndServe(":9999", nil)
if err != nil {
log.Fatal(err)
}
}
在浏览器输入:
http://localhost:9999/hello
运行结果:
hello world
使用Handler实现并发处理
type countHandler struct {
mu sync.Mutex // guards n
n int
}
func (h *countHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mu.Lock()
defer h.mu.Unlock()
h.n++
fmt.Fprintf(w, "count is %d\n", h.n)
}
func testHttpServer2() {
http.Handle("/count", new(countHandler))
log.Fatal(http.ListenAndServe(":8080", nil))
}
在浏览器输入:http://localhost:8080/count,刷新查看结果
count is 8