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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
package mq
import (
"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"time"
)
// 获取当前时间戳(毫秒)
func GetCurrentMillisecond() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
// 获取当前时间戳(秒)
func GetCurrentUnixMicro() int64 {
return time.Now().Unix() * 1000
}
// 对字符串进行sha1 计算
func Sha1(data string) string {
t := sha1.New()
io.WriteString(t, data)
return fmt.Sprintf("%x", t.Sum(nil))
}
// 对数据进行md5计算
func Md5(byteMessage []byte) string {
h := md5.New()
h.Write(byteMessage)
return hex.EncodeToString(h.Sum(nil))
}
func HamSha1(data string, key []byte) string {
hmac := hmac.New(sha1.New, key)
hmac.Write([]byte(data))
return base64.StdEncoding.EncodeToString(hmac.Sum(nil))
}
func dump(data interface{}) {
fmt.Println(data)
}
// POST请求
func httpPost(urlStr string, header map[string]string, body []byte) ([]byte, int, error) {
client := &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, time.Second*2)
if err != nil {
return nil, err
}
conn.SetDeadline(time.Now().Add(time.Second * 2))
return conn, nil
},
ResponseHeaderTimeout: time.Second * 2,
},
}
req, err := http.NewRequest("POST", urlStr, bytes.NewBuffer([]byte(body)))
if err != nil {
return []byte(""), 0, err
}
req.Header.Set("Content-Type", "text/html;charset=utf-8")
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
for k, v := range header {
req.Header.Set(k, v)
}
resp, err := client.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 204 {
return nil, resp.StatusCode, nil
}
data, err := ioutil.ReadAll(resp.Body)
return data, resp.StatusCode, err
}
// GET请求
func HttpGet(urlStr string, header map[string]string) ([]byte, int, error) {
client := &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, time.Second*30)
if err != nil {
return nil, err
}
conn.SetDeadline(time.Now().Add(time.Second * 30))
return conn, nil
},
ResponseHeaderTimeout: time.Second * 30,
},
}
req, _ := http.NewRequest("GET", urlStr, nil)
req.Header.Set("Content-Type", "text/html;charset=utf-8")
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
for k, v := range header {
req.Header.Set(k, v)
}
resp, err := client.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 204 {
return nil, resp.StatusCode, nil
}
data, err := ioutil.ReadAll(resp.Body)
return data, resp.StatusCode, err
}
// GET请求
func HttpDelete(urlStr string, header map[string]string) ([]byte, int, error) {
client := &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, time.Second*2)
if err != nil {
return nil, err
}
conn.SetDeadline(time.Now().Add(time.Second * 2))
return conn, nil
},
ResponseHeaderTimeout: time.Second * 2,
},
}
req, _ := http.NewRequest("DELETE", urlStr, nil)
req.Header.Set("Content-Type", "text/html;charset=utf-8")
for k, v := range header {
req.Header.Set(k, v)
}
resp, err := client.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 204 {
return nil, resp.StatusCode, nil
}
data, err := ioutil.ReadAll(resp.Body)
return data, resp.StatusCode, err
}
|