http.Get()
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func testGet() {
key := "***********************"
url := "http://apis.juhe.cn/simpleWeather/query?city=北京&key=" + key
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))
}
func main() {
testGet()
}
把参数改为变量,而不是拼接在后面
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func testGet() {
params := url.Values{}
u, err := url.Parse("http://apis.juhe.cn/simpleWeather/query")
if err != nil {
log.Fatal(err)
}
params.Set("key", "**************************")
params.Set("city", "北京")
u.RawQuery = params.Encode()
urlPath := u.String()
fmt.Printf("urlPath: %v\n", urlPath)
r, err1 := http.Get(urlPath)
if err1 != nil {
log.Fatal(err1)
}
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
fmt.Printf("b: %v\n", string(b))
}
func main() {
testGet()
}
解析json后
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func testJson() {
type result struct {
Reason string `json:"reason"`
Result map[string]interface{} `json:"result"`
ErrorCode int `json:"error_code"`
}
params := url.Values{}
u, err := url.Parse("http://apis.juhe.cn/simpleWeather/query")
if err != nil {
log.Fatal(err)
}
params.Set("key", "********************************")
params.Set("city", "北京")
u.RawQuery = params.Encode()
urlPath := u.String()
fmt.Printf("urlPath: %v\n", urlPath)
r, err1 := http.Get(urlPath)
if err1 != nil {
log.Fatal(err1)
}
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
var res result
json.Unmarshal(b, &res)
fmt.Printf("res: %+v\n", res)
}
func main() {
testJson()
}
整理个时候数据如下:
{
Reason:查询成功!
Result:map[
city:北京
future:[
map[
date:2022-03-14
direct:西风转东风
temperature:5/18℃
weather:晴
wid:map[day:00 night:00]
]
map[date:2022-03-15 direct:北风转东北风 temperature:5/17℃ weather:晴转多云 wid:map[day:00 night:01]] '
map[date:2022-03-16 direct:东风 temperature:3/12℃ weather:多云转阴 wid:map[day:01 night:02]]
map[date:2022-03-17 direct:东风转东南风 temperature:0/4℃ weather:小雨转雨夹雪 wid:map[day:07 night:06]]
map[date:2022-03-18 direct:北风转东北风 temperature:-2/4℃ weather:阴 wid:map[day:02 night:02]]
]
realtime:map[aqi:33 direct:西南风 humidity:20 info:晴 power:4级 temperature:18 wid:00]]
ErrorCode:0
}
测试http请求网址 http://httpbin.org/get
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
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, _ := ioutil.ReadAll(r.Body)
var res Result
json.Unmarshal(b, &res)
fmt.Printf("res: %#v\n", res)
}
func main() {
testGet()
}
设置headers
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func testGet() {
type Result struct {
Args string `json:"args"`
Headers map[string]string `json:"headers"`
Origin string `json:"origin"`
Url string `json:"url"`
}
c := &http.Client{}
r, _ := http.NewRequest("GET", "http://httpbin.org/get", nil)
r.Header.Add("name", "xf")
r.Header.Add("age", "18")
r2, _ := c.Do(r)
defer r2.Body.Close()
b, _ := ioutil.ReadAll(r2.Body)
var res Result
json.Unmarshal(b, &res)
fmt.Printf("res: %#v\n", res)
}
func main() {
testGet()
}
http.post()
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
func testPost() {
params := url.Values{}
urlPath := "http://apis.juhe.cn/simpleWeather/query"
params.Add("key", "********************************")
params.Add("city", "北京")
r, err1 := http.PostForm(urlPath, params)
if err1 != nil {
log.Fatal(err1)
}
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
fmt.Printf("b: %v\n", string(b))
}
func main() {
testPost()
}
另一种形式
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
func testPost() {
params := url.Values{
"name": {"xf"},
"age": {"18"},
}
s := params.Encode()
r, _ := http.Post("http://httpbin.org/post", "text/html", strings.NewReader(s))
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
fmt.Printf("b: %v\n", string(b))
}
func main() {
testPost()
}
发送json数据
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func testPost() {
data := make(map[string]interface{})
data["name"] = "xf"
data["age"] = 18
b2, _ := json.Marshal(data)
r, _ := http.Post("http://httpbin.org/post", "application/json", bytes.NewReader(b2))
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
fmt.Printf("b: %v\n", string(b))
}
func main() {
testPost()
}
server功能
package main
import (
"io"
"net/http"
)
func testHttpServer() {
f := func(resp http.ResponseWriter, req *http.Request) {
io.WriteString(resp, "hello world")
}
http.HandleFunc("/hello", f)
http.ListenAndServe(":9999", nil)
}
func main() {
testHttpServer()
}
|