我们这里是通过网站聚合数据获取到的公共API接口,也可以直接访问https://baidu.com来进行测试,但是效果不佳
下文中访问url所用到的key需要自己去聚合数据申请,也可以直接使用https://baidu.com代替
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func testGet() {
url := "http://apis.juhe.cn/fapig/nba/query?key="*********""
//通过http.get建立网络连接获取数据
r, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
//关闭链接连接
defer r.Body.Close()
//格式化我们拿到的数据
b, err2 := ioutil.ReadAll(r.Body)
if err2 != nil {
log.Fatal(err2)
}
fmt.Printf("b: %v\n", string(b))
}
func main() {
testGet()
}
b: {"reason":"查询成功","result":{"title":"美国职业篮球联赛","duration":"2021-2022","matchs":[{"date":"2022-06-17","week":"周五","list":[{"time_start":"09:00","status":"3","status_text":"完赛","team1":"金州勇士","team2":"波士顿凯尔特人","team1_score":"103","team2_score":"90"}]}]},"error_code":0}
如果用上述的方法的话,我们需要把所有的参数一个个直接拼接在url后,非常不方便也不美观,所以我们可以调用url.Values库来达到我们想要的效果
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func testGet() {
params := url.Values{}
Url, _ := url.Parse("http://apis.juhe.cn/simpleWeather/query")
params.Set("key", "***********")
params.Set("city", "深圳")
//参数有中文,进行转码
Url.RawQuery = params.Encode()
urlPath := Url.String()
r, err := http.Get(urlPath)
if err != nil {
log.Fatal(err)
}
defer r.Body.Close()
b, err2 := ioutil.ReadAll(r.Body)
if err2 != nil {
log.Fatal(err2)
}
fmt.Printf("b: %v\n", string(b))
}
func main() {
testGet()
}
解析json结果数据的大概思路就是,先定义一个结构体,结构体中定义我们想要的字段名和字段类型,然后通过json.Unmarshal方法进行解析
func testGet() {
type result struct {
Args string `json:"args"`
Headers map[string]string `json:"headers"`
Origin string `json:"origin"`
Url string `json:"url"`
}
r, err := http.Get("http://httpbin.org/get")
if err != nil {
log.Fatal(err)
}
defer r.Body.Close()
b, err2 := ioutil.ReadAll(r.Body)
if err2 != nil {
log.Fatal(err2)
}
var res result
_ = json.Unmarshal(b, &res)
fmt.Printf("b: %v\n", string(b))
fmt.Printf("res: %#v\n", res)
}
http://httpbin.org/get网站是一个专门测验网络请求头的一个开源接口
得到数据
b: {
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Host": "httpbin.org",
"User-Agent": "Go-http-client/1.1",
"X-Amzn-Trace-Id": "Root=1-62b1643e-14da6e0c6b8484091d3829d9"
},
"origin": "34.217.130.230",
"url": "http://httpbin.org/get"
}
res: 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-62b1643e-14da6e0c6b8484091d3829d9"}, Origin:"34.217.130.230", Url:"http://httpbin.org/get"}
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func testGet() {
client := &http.Client{}
//得到请求信息req
req, _ := http.NewRequest("GET", "http://httpbin.org/get", nil)
//设置请求头
req.Header.Add("name", "lyizriii")
req.Header.Add("age", "22")
resp, _ := client.Do(req)
b, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("string(b): %v\n", string(b))
}
func main() {
testGet()
}
响应结果
string(b): {
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Age": "22",
"Host": "httpbin.org",
"Name": "lyizriii",
"User-Agent": "Go-http-client/1.1",
"X-Amzn-Trace-Id": "Root=1-62b166b5-6a73b006211098db470a3280"
},
"origin": "34.217.130.230",
"url": "http://httpbin.org/get"
}
可以看到 name和age已经被我们添加到请求头中了
建立一个post连接
post请求不同于get请求,需要将params跟在url后面
post请求的参数和url是作为两个参数存在的
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func testPost() {
urlStr := "http://apis.juhe.cn/simpleWeather/query"
values := url.Values{}
values.Add("city", "深圳")
values.Add("key", "*****")
//建立请求链接
r, _ := http.PostForm(urlStr, values)
defer r.Body.Close()
//读取buffer
b, _ := ioutil.ReadAll(r.Body)
fmt.Printf("b: %v\n", string(b))
}
func main() {
testPost()
}
过程和上边的差不多,只不过要先建立一个map(json),然后将json转化为byte,再将byte转化为io.Reader类型
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func testPost() {
values := make(map[string]interface{})
values["name"] = "lyizrii"
values["age"] = "22"
//将json转化为字节数据
byteValues, _ := json.Marshal(values)
r, _ := http.Post("http://httpbin.org/post", "application/html", bytes.NewReader(byteValues))
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
fmt.Printf("b: %v\n", string(b))
}
func main() {
testPost()
}
响应结果
b: {
"args": {},
"data": "{\"age\":\"22\",\"name\":\"lyizrii\"}",
"files": {},
"form": {},
"headers": {
"Accept-Encoding": "gzip",
"Content-Length": "29",
"Content-Type": "application/html",
"Host": "httpbin.org",
"User-Agent": "Go-http-client/1.1",
"X-Amzn-Trace-Id": "Root=1-62b17d89-5b9af2da0003537d30f90137"
},
"json": {
"age": "22",
"name": "lyizrii"
},
"origin": "54.186.169.115",
"url": "http://httpbin.org/post"
}
package main
import (
"io"
"net/http"
)
func testServer() {
f := func(resq http.ResponseWriter, req *http.Request) {
//写入一个string文件
io.WriteString(resq, "hello go!")
}
//
http.HandleFunc("/", f)
http.ListenAndServe(":9999", nil)
}
func main() {
testServer()
}
打开网页localhost:9999,就能看到hello go!的文字了
在处理并发时,为了防止多端修改数据造成紊乱,使用锁的方法进行解决
package main
import (
"fmt"
"log"
"net/http"
"sync"
)
type countHeader struct {
mu sync.Mutex
n int
}
//ServeHTTP 这个函数不能改名字,必须严格遵守,这是Handle的要求,这块是个大坑
func (h *countHeader) 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 testHttpServer() {
count := new(countHeader)
fmt.Printf("count: %v\n", count)
http.Handle("/hello", count)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func main() {
testHttpServer()
}
运行之后,访问localhost:8080/hello 我们访问页面会发现有count is 0的字样,并且每刷新一次就加1