package util import ( "bytes" "encoding/json" "errors" "fmt" "io" "net/http" "strconv" "time" "gat1400Exchange/pkg/logger" "gat1400Exchange/vo" ) func SendData(payload []byte, url string) bool { body, err := HttpPost(url, nil, payload) if err != nil { logger.Error("Post request failure, url:%s, err:%s", url, err.Error()) return false } var rsp vo.SyncServerResponse err = json.Unmarshal(body, &rsp) if err != nil { logger.Error("Can't parse response, url:%s, rsp: %v", url, body) return false } return rsp.Success } func HttpPost(url string, header map[string]string, data []byte) ([]byte, error) { req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) if err != nil { return nil, err } if header != nil { for k, v := range header { req.Header.Set(k, v) } } cli := &http.Client{} resp, err := cli.Do(req) if err != nil { return nil, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil } func HttpPut(url string, header map[string]string, data []byte) ([]byte, error) { req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data)) if err != nil { return nil, err } //req.Header.Set("Content-Type", "applicaiton/json; charset=UTF-8") if header != nil { for k, v := range header { req.Header.Set(k, v) } } cli := &http.Client{} resp, err := cli.Do(req) if err != nil { return nil, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil } func HttpGet(url string, header map[string]string) ([]byte, error) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return nil, err } //req.Header.Set("Content-Type", "applicaiton/json; charset=UTF-8") if header != nil { for k, v := range header { req.Header.Set(k, v) } } cli := &http.Client{} resp, err := cli.Do(req) if err != nil { return nil, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil } func ImageDownload(downloadUrl string, fb func(length, downLen int64)) ([]byte, error) { var ( fSize int64 buf = make([]byte, 10*1024) buffer bytes.Buffer written int64 ) request, err := http.NewRequest("GET", downloadUrl, nil) if err != nil { return nil, err } //创建一个http client client := new(http.Client) client.Timeout = time.Second * 600 //设置超时时间 // get方法获取资源 resp, err := client.Do(request) if err != nil { return nil, err } if resp.StatusCode != 200 { return nil, errors.New(fmt.Sprintf("请求失败, 状态码:%d", resp.StatusCode)) } //读取服务器返回的文件大小 fSize, err = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 32) if err != nil { return nil, err } if resp.Body == nil { return nil, errors.New("body is null") } defer resp.Body.Close() //下面是 io.copyBuffer() 的简化版本 for { //读取bytes nr, er := resp.Body.Read(buf) if nr > 0 { //写入bytes nw, ew := buffer.Write(buf[0:nr]) //数据长度大于0 if nw > 0 { written += int64(nw) } //写入出错 if ew != nil { err = ew break } //读取是数据长度不等于写入的数据长度 if nr != nw { err = io.ErrShortWrite break } } if er != nil { if er != io.EOF { err = er } break } //没有错误了快使用 callback if fb != nil { fb(fSize, written) } } return buffer.Bytes(), err }