Golang发起GET POST请求

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)


func httpGet() {
	//func Get(url string) (resp *Response, err error)
	//url := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx1a7cbefc73f1d9e1&secret=e7158342d3fab1742877dfb77dd40dae"
	url := "http://127.0.0.1:8008/taskinfo/state"

	resp, _ := http.Get(url)

	defer resp.Body.Close()              //一定要关闭返回的response中的body
	body, _ := ioutil.ReadAll(resp.Body) //读取body中的信息

	//转换json
	//var res interface{}
	//err := json.Unmarshal(body, &res)
	//if err != nil {
	//	panic(err)
	//}
	//字符串输出
	res := string(body)

	fmt.Println(res,"0000")
}


func httpPost() {
	//url := "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send"
	url := "http://127.0.0.1:8008/taskinfo/groups"
	var data = make(map[string]interface{})

	data["data"] = map[string]interface{}{
		"thing1":map[string]string{
			"value":"活动打卡",
		},
		"time11":map[string]string{
			"value": "2015年01月05日",
		},
	}

	// 超时时间:5秒
	client := &http.Client{Timeout: 5 * time.Second}
	jsonStr, _ := json.Marshal(data)
	resp, _ := client.Post(url, "application/json", bytes.NewBuffer(jsonStr))

	defer resp.Body.Close()

	result, _ := ioutil.ReadAll(resp.Body)
	res := string(result)
	fmt.Println(res)
}


func main() {
	//httpGet()
	httpPost()

}

 

你可能感兴趣的:(Go,go)