-
Notifications
You must be signed in to change notification settings - Fork 3
/
httpbase.go
44 lines (42 loc) · 850 Bytes
/
httpbase.go
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
package mzpush
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
func doPost(url string, form url.Values) ([]byte, error) {
var result []byte
var req *http.Request
var resp *http.Response
var err error
req, err = http.NewRequest("POST", url, strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
tryTime := 0
tryAgain:
resp, err = client.Do(req)
if err != nil {
fmt.Println("meizu push post err", err, tryTime)
tryTime += 1
if tryTime < 3 {
goto tryAgain
}
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
result, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
str := string(result)
str, err = strconv.Unquote(str)
if err != nil {
str = string(result)
}
return []byte(str), nil
}