| 1 |
package mq |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/hmac" |
| 6 |
"crypto/md5" |
| 7 |
"crypto/sha1" |
| 8 |
"encoding/base64" |
| 9 |
"encoding/hex" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"io/ioutil" |
| 13 |
"net" |
| 14 |
"net/http" |
| 15 |
|
| 16 |
"time" |
| 17 |
) |
| 18 |
|
| 19 |
前时间戳(毫秒) |
| 20 |
func GetCurrentMillisecond() int64 { |
| 21 |
return time.Now().UnixNano() / int64(time.Millisecond) |
| 22 |
} |
| 23 |
|
| 24 |
前时间戳(秒) |
| 25 |
func GetCurrentUnixMicro() int64 { |
| 26 |
return time.Now().Unix() * 1000 |
| 27 |
} |
| 28 |
|
| 29 |
进行sha1 计算 |
| 30 |
func Sha1(data string) string { |
| 31 |
t := sha1.New() |
| 32 |
io.WriteString(t, data) |
| 33 |
return fmt.Sprintf("%x", t.Sum(nil)) |
| 34 |
} |
| 35 |
|
| 36 |
进行md5计算 |
| 37 |
func Md5(byteMessage []byte) string { |
| 38 |
h := md5.New() |
| 39 |
h.Write(byteMessage) |
| 40 |
return hex.EncodeToString(h.Sum(nil)) |
| 41 |
} |
| 42 |
|
| 43 |
func HamSha1(data string, key []byte) string { |
| 44 |
hmac := hmac.New(sha1.New, key) |
| 45 |
hmac.Write([]byte(data)) |
| 46 |
|
| 47 |
return base64.StdEncoding.EncodeToString(hmac.Sum(nil)) |
| 48 |
} |
| 49 |
|
| 50 |
func dump(data interface{}) { |
| 51 |
fmt.Println(data) |
| 52 |
} |
| 53 |
|
| 54 |
请求 |
| 55 |
func httpPost(urlStr string, header map[string]string, body []byte) ([]byte, int, error) { |
| 56 |
|
| 57 |
client := &http.Client{ |
| 58 |
Transport: &http.Transport{ |
| 59 |
Dial: func(netw, addr string) (net.Conn, error) { |
| 60 |
conn, err := net.DialTimeout(netw, addr, time.Second*2) |
| 61 |
if err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
conn.SetDeadline(time.Now().Add(time.Second * 2)) |
| 65 |
return conn, nil |
| 66 |
}, |
| 67 |
ResponseHeaderTimeout: time.Second * 2, |
| 68 |
}, |
| 69 |
} |
| 70 |
|
| 71 |
req, err := http.NewRequest("POST", urlStr, bytes.NewBuffer([]byte(body))) |
| 72 |
if err != nil { |
| 73 |
return []byte(""), 0, err |
| 74 |
} |
| 75 |
|
| 76 |
req.Header.Set("Content-Type", "text/html;charset=utf-8") |
| 77 |
|
| 78 |
for k, v := range header { |
| 79 |
req.Header.Set(k, v) |
| 80 |
} |
| 81 |
|
| 82 |
resp, err := client.Do(req) |
| 83 |
if err != nil { |
| 84 |
return nil, 0, err |
| 85 |
} |
| 86 |
defer resp.Body.Close() |
| 87 |
if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 204 { |
| 88 |
return nil, resp.StatusCode, nil |
| 89 |
} |
| 90 |
|
| 91 |
data, err := ioutil.ReadAll(resp.Body) |
| 92 |
return data, resp.StatusCode, err |
| 93 |
} |
| 94 |
|
| 95 |
请求 |
| 96 |
func HttpGet(urlStr string, header map[string]string) ([]byte, int, error) { |
| 97 |
|
| 98 |
client := &http.Client{ |
| 99 |
Transport: &http.Transport{ |
| 100 |
Dial: func(netw, addr string) (net.Conn, error) { |
| 101 |
conn, err := net.DialTimeout(netw, addr, time.Second*30) |
| 102 |
if err != nil { |
| 103 |
return nil, err |
| 104 |
} |
| 105 |
conn.SetDeadline(time.Now().Add(time.Second * 30)) |
| 106 |
return conn, nil |
| 107 |
}, |
| 108 |
ResponseHeaderTimeout: time.Second * 30, |
| 109 |
}, |
| 110 |
} |
| 111 |
req, _ := http.NewRequest("GET", urlStr, nil) |
| 112 |
|
| 113 |
req.Header.Set("Content-Type", "text/html;charset=utf-8") |
| 114 |
|
| 115 |
for k, v := range header { |
| 116 |
req.Header.Set(k, v) |
| 117 |
} |
| 118 |
|
| 119 |
resp, err := client.Do(req) |
| 120 |
if err != nil { |
| 121 |
return nil, 0, err |
| 122 |
} |
| 123 |
defer resp.Body.Close() |
| 124 |
|
| 125 |
if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 204 { |
| 126 |
return nil, resp.StatusCode, nil |
| 127 |
} |
| 128 |
|
| 129 |
data, err := ioutil.ReadAll(resp.Body) |
| 130 |
return data, resp.StatusCode, err |
| 131 |
} |
| 132 |
|
| 133 |
请求 |
| 134 |
func HttpDelete(urlStr string, header map[string]string) ([]byte, int, error) { |
| 135 |
|
| 136 |
client := &http.Client{ |
| 137 |
Transport: &http.Transport{ |
| 138 |
Dial: func(netw, addr string) (net.Conn, error) { |
| 139 |
conn, err := net.DialTimeout(netw, addr, time.Second*2) |
| 140 |
if err != nil { |
| 141 |
return nil, err |
| 142 |
} |
| 143 |
conn.SetDeadline(time.Now().Add(time.Second * 2)) |
| 144 |
return conn, nil |
| 145 |
}, |
| 146 |
ResponseHeaderTimeout: time.Second * 2, |
| 147 |
}, |
| 148 |
} |
| 149 |
req, _ := http.NewRequest("DELETE", urlStr, nil) |
| 150 |
|
| 151 |
req.Header.Set("Content-Type", "text/html;charset=utf-8") |
| 152 |
for k, v := range header { |
| 153 |
req.Header.Set(k, v) |
| 154 |
} |
| 155 |
|
| 156 |
resp, err := client.Do(req) |
| 157 |
if err != nil { |
| 158 |
return nil, 0, err |
| 159 |
} |
| 160 |
defer resp.Body.Close() |
| 161 |
|
| 162 |
if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 204 { |
| 163 |
return nil, resp.StatusCode, nil |
| 164 |
} |
| 165 |
|
| 166 |
data, err := ioutil.ReadAll(resp.Body) |
| 167 |
return data, resp.StatusCode, err |
| 168 |
} |