get请求带参数,请求头
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
)
type ResultData struct{
Reason string `json:"reason"`
Result DataList `json:"result"`
Error_code int `json:"error_code"`
}
type DataList struct{
Data []DataInfo `json:"data"`
}
type DataInfo struct{
Content string `json:"content"`
HashId string `json:"hashId"`
Unixtime int `json:"unixtime"`
Updatetime string `json:"updatetime"`
}
func main() {
key := "#####################"
tunix := strconv.Itoa(int(time.Now().Unix()))
request, err := http.NewRequest("GET", "http://v.juhe.cn/joke/content/list.php", nil)
if err != nil{
log.Println("err->", err)
}
// 添加请求头
request.Header.Add("content-type", "application/x-www-form-urlencoded")
request.Header.Add("cache-control", "no-cache")
//加入get参数
q := request.URL.Query()
q.Add("sort", "desc")
q.Add("time", tunix)
q.Add("key", key)
fmt.Println("q->", q)
request.URL.RawQuery = q.Encode()
fmt.Println("encode->", q.Encode())
resp, err := http.DefaultClient.Do(request)
if err != nil{
log.Println("err->", err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil{
log.Println("err->", err)
}
rdata := string(data)
fmt.Println(rdata)
var resultdata ResultData
json.Unmarshal([]byte(rdata), &resultdata)
fmt.Printf("%s\n", resultdata)
}
有疑问加站长微信联系(非本文作者)
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)