zhangzengfei
2023-05-22 acb15a431beb73159921477415c5d0f7d06c6768
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
package ipc
 
import (
    "bytes"
    "io/ioutil"
    "net/http"
    "time"
 
    "basic.com/valib/logger.git"
)
 
func HttpGet(url string, parama []byte) (body []byte, err error) {
    return request("GET", url, parama)
}
 
func HttpPost(url string, parama []byte) (body []byte, err error) {
    return request("POST", url, parama)
}
 
func request(method string, url string, parama []byte) (body []byte, err error) {
    timeout := time.Duration(10 * time.Second)
    client := http.Client{
        Timeout: timeout,
    }
    request, err := http.NewRequest(method, url, bytes.NewBuffer(parama))
 
    request.Header.Set("Content-type", "application/json")
 
    if err != nil {
        logger.Warn("build request fail !")
        return nil, err
    }
 
    resp, err := client.Do(request)
    if err != nil {
        logger.Warn("request error: ", err)
        return nil, err
    }
 
    defer resp.Body.Close()
 
    body, err = ioutil.ReadAll(resp.Body)
    if err != nil {
        logger.Warn(err)
    }
 
    return body, nil
}