1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package http
- import (
- "net/http"
- "strings"
- "time"
- "kpt.notice/pkg/log"
- )
- type Client struct {
- client *http.Client
- }
- func NewClient(timeout time.Duration) *Client {
- return &Client{
- client: &http.Client{
- Timeout: timeout,
- },
- }
- }
- func HttpPost(url, contentType, body string) *http.Response {
- // body := `{"userinfo":"tmr.shengmu23.bbc","openid":"123"}`
- // url :="http://kpttest.kptyun.com/userwxopenid/binding"
- // contentType:="application/json"
- log.Errorf("HttpPost====enter body=======%+v\n", body)
- resp, err := http.Post(url, contentType, strings.NewReader(body))
- if err != nil || resp.StatusCode != 200 {
- log.Errorf("post err ====%+v===%+v\n", err, resp.StatusCode)
- return nil
- }
- defer resp.Body.Close()
- log.Errorf("resp=====%+v\n", resp.StatusCode)
- return resp
- }
- func HttpGet(url string, params map[string]string) *http.Response {
- // url :="http://kpttest.kptyun.com/userwxopenid/binding"
- // contentType:="application/json"
- // params := map[string]string{"userinfo":"tmr.shengmu23.bbc","openid":"123"}
- s := make([]string, len(params))
- for k, v := range params {
- s = append(s, k+"="+v)
- }
- url = url + "?" + strings.Join(s, "&")
- resp, err := http.Get(url)
- if err != nil || resp.StatusCode != 200 {
- return nil
- }
- defer resp.Body.Close()
- return resp
- }
|