http.go 2.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
package rpc

import (
	"git.hilo.cn/hilo-common/domain"
	"io/ioutil"
	"net"
	"net/http"
	"net/url"
	"strings"
)

var httpTransport = &http.Transport{
	MaxIdleConnsPerHost: defaultMaxIdleConnsPerHost,
	MaxIdleConns:        defaultMaxIdleConns,
	Proxy:               http.ProxyFromEnvironment,
	DialContext: (&net.Dialer{
		Timeout:   defaultDialTimeout,
		KeepAlive: defaultKeepAliveTimeout,
	}).DialContext,
	IdleConnTimeout:       defaultIdleConnTimeout,
	TLSHandshakeTimeout:   defaultTLSHandshakeTimeout,
	ExpectContinueTimeout: defaultExpectContinueTimeout,
}

// 执行http get请求
// param: header + postForm
// return: resp body,err
func HttpGet(model *domain.Model, _url string, header map[string]string, query map[string][]string) ([]byte, error) {
	var err error
	defer func() {
		if err != nil {
			model.Log.Errorf("HttpPut err:%v", err)
		}
	}()
	if len(query) > 0 {
		q := url.Values{}
		for k, v := range query {
			for _, v2 := range v {
				q.Add(k, v2)
			}
		}
		_url += "?" + q.Encode()
	}
	req, err := http.NewRequest("GET", _url, nil)
	if err != nil {
		return nil, err
	}
	for k, v := range header {
		req.Header.Add(k, v)
	}
	resp, err := httpTransport.RoundTrip(req)
	if err != nil {
		return nil, err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	if resp != nil {
		err = resp.Body.Close()
	}
	return body, err
}

// 执行http post请求
// 默认: Content-Type":"application/x-www-form-urlencoded"
// param: header + postForm
// return: resp body,err
func HttpPostForm(model *domain.Model, _url string, header, form map[string]string) ([]byte, error) {
	return httpForm(model, "POST", _url, header, form)
}

// 执行http put请求
// 默认: Content-Type":"application/x-www-form-urlencoded"
// param: header + postForm
// return: resp body,err
func HttpPutForm(model *domain.Model, _url string, header map[string]string, form map[string]string) ([]byte, error) {
	return httpForm(model, "PUT", _url, header, form)
}

// 默认: Content-Type":"application/x-www-form-urlencoded"
// param: header + postForm
// return: resp body,err
func httpForm(model *domain.Model, method, _url string, header map[string]string, form map[string]string) ([]byte, error) {
	var err error
	defer func() {
		if err != nil {
			model.Log.Errorf("HttpPut err:%v", err)
		}
	}()
	postForm := url.Values{}
	for k, v := range form {
		postForm.Set(k, v)
	}
	payload := strings.NewReader(postForm.Encode())
	req, err := http.NewRequest(method, _url, payload)
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	if err != nil {
		return nil, err
	}
	for k, v := range header {
		req.Header.Add(k, v)
	}
	resp, err := httpTransport.RoundTrip(req)
	if err != nil {
		return nil, err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	if resp != nil {
		err = resp.Body.Close()
	}
	return body, err
}