数据上报
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

257 lines
6.4 KiB

package dbHelper
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"strings"
"time"
)
type HttpHelper struct {
Url string
Token string
client http.Client
}
func (the *HttpHelper) Initial() {
the.client = http.Client{}
time.Sleep(time.Second * 1)
}
func (the *HttpHelper) HttpGet(queryBody string) string {
url := the.Url
client := the.client
req, err := http.NewRequest("GET", url, strings.NewReader(queryBody))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
fmt.Println("http get 请求,url", url, " <- code=", resp.StatusCode)
body, err := io.ReadAll(resp.Body)
return string(body)
}
func (the *HttpHelper) HttpGetWithHeader(queryBody string, headerMap map[string]string) string {
url := the.Url
client := the.client
req, err := http.NewRequest("GET", url, strings.NewReader(queryBody))
req.Header.Set("Content-Type", "application/json")
for k, v := range headerMap {
req.Header.Set(k, v)
}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer func(resp *http.Response) {
if resp != nil && resp.Body != nil {
errClose := resp.Body.Close()
if errClose != nil {
log.Printf("关闭异常")
}
}
}(resp)
fmt.Println("http get 请求,url", url, " <- code=", resp.StatusCode)
body, err := io.ReadAll(resp.Body)
return string(body)
}
func (the *HttpHelper) Publish(messageBytes []byte) (string, error) {
url := the.Url
client := the.client
req, err := http.NewRequest("POST", url, bytes.NewReader(messageBytes))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Println("请求POST异常 ", err, resp)
return "", err
}
defer resp.Body.Close()
log.Println("http post 请求,url", url, " <- code=", resp.StatusCode)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("请求ReadAll异常 ", err, resp)
return "", err
}
return string(body), err
}
func (the *HttpHelper) PublishWithHeader(messageBytes []byte, headers map[string]string) (string, error) {
resp, err := HttpPostWithHeader(the.Url, string(messageBytes), headers)
if err != nil {
log.Printf("数据推送异常 err=%s,resp=%s", err.Error(), resp)
}
return resp, err
}
// 静态方法
func HttpGet(url string, queryBody string) string {
client := &http.Client{}
req, err := http.NewRequest("GET", url, strings.NewReader(queryBody))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
fmt.Println("http get 请求,url", url, " <- code=", resp.StatusCode)
body, err := io.ReadAll(resp.Body)
return string(body)
}
func HttpPost(url string, queryBody string) (string, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", url, strings.NewReader(queryBody))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Println("请求POST异常 ", err, resp)
return "", err
}
defer resp.Body.Close()
fmt.Println("http post 请求,url", url, " <- code=", resp.StatusCode)
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("请求ReadAll异常 ", err, resp)
return "", err
}
return string(body), err
}
func HttpPostWithHeader(url string, queryBody string, headers map[string]string) (string, error) {
tr := &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
req, err := http.NewRequest("POST", url, strings.NewReader(queryBody))
req.Header.Set("Content-Type", "application/json")
for k, v := range headers {
req.Header.Add(k, v)
}
fmt.Printf("http post 开始请求,%s,\n,%v \n", url, req)
resp, err := client.Do(req)
if err != nil {
fmt.Println("请求POST异常 ", err, resp)
return "", err
}
defer resp.Body.Close()
//fmt.Println("http post 请求,url=", url, " <- code=", resp.StatusCode)
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("请求ReadAll异常 ", err, resp)
return "", err
}
return string(body), err
}
func HttpPostFormDataWithHeader(url string, queryBody string, headers map[string]string) (string, error) {
tr := &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
req, err := http.NewRequest("POST", url, strings.NewReader(queryBody))
req.Header.Set("Content-Type", "application/json")
for k, v := range headers {
req.Header.Add(k, v)
}
fmt.Printf("http post 开始请求,%s,\n,%s \n", url, req)
resp, err := client.Do(req)
if err != nil {
fmt.Println("请求POST异常 ", err, resp)
return "", err
}
defer resp.Body.Close()
fmt.Println("http post 请求,url=", url, " <- code=", resp.StatusCode)
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("请求ReadAll异常 ", err, resp)
return "", err
}
return string(body), err
}
func UploadFile(url string, headers map[string]string, bodyParams map[string]string, fileName string) ([]byte, error) {
body := new(bytes.Buffer)
// 创建 multipart writer
mpWriter := multipart.NewWriter(body)
for key, val := range bodyParams {
_ = mpWriter.WriteField(key, val)
}
file, err := os.Open(fileName)
fileInfo, err := file.Stat()
// 添加文件字段
formFile, err := mpWriter.CreateFormFile("File", fileInfo.Name())
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
if err != nil {
log.Printf("无法打开文件[%s]: err=%s", file.Name(), err.Error())
return nil, err
}
//读取文件并写入请求体
_, err = io.Copy(formFile, file)
if err != nil {
return nil, err
}
err = mpWriter.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, body)
//req.Header.Set("Content-Type", "application/json")
//req.Header.Set("Content-Type","multipart/form-data")
req.Header.Add("Content-Type", mpWriter.FormDataContentType())
for k, v := range headers {
req.Header.Add(k, v)
}
if err != nil {
return nil, err
}
//tr := &http.Transport{
// DisableKeepAlives: true,
// TLSClientConfig: &tls.IOConfig{InsecureSkipVerify: true},
//}
//HttpClient := &http.Client{Transport: tr}
HttpClient := &http.Client{}
resp, err := HttpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
content, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return content, nil
}