| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- package util
- import (
- "bytes"
- "crypto/tls"
- "encoding/pem"
- "fmt"
- "github.com/goccy/go-json"
- "golang.org/x/crypto/pkcs12"
- "io"
- "io/ioutil"
- "log"
- "mime/multipart"
- "net/http"
- "os"
- "time"
- )
- func HttpPostAuth(url string, data string, Auth string) (*http.Response, error) {
- //sendbuf, _ := sonic.Marshal(data)
- req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte(data)))
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Authorization", Auth)
- client := &http.Client{
- Timeout: 3 * time.Second,
- }
- return client.Do(req)
- }
- func HttpPostForm(url string, data string) (*http.Response, error) {
- req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte(data)))
- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
- client := &http.Client{
- Timeout: 3 * time.Second,
- }
- return client.Do(req)
- }
- // HTTPGet get 请求
- func HTTPGet(uri string) ([]byte, error) {
- response, err := http.Get(uri)
- if err != nil {
- return nil, err
- }
- defer response.Body.Close()
- if response.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
- }
- return ioutil.ReadAll(response.Body)
- }
- // HTTPPost post 请求
- func HTTPPost(uri string, data string) ([]byte, error) {
- body := bytes.NewBuffer([]byte(data))
- response, err := http.Post(uri, "application/json", body)
- if err != nil {
- return nil, err
- }
- defer response.Body.Close()
- if response.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
- }
- return ioutil.ReadAll(response.Body)
- }
- func HttpPostSaas(uri string, data string, times int) ([]byte, error) {
- client := &http.Client{
- Timeout: time.Second * 5, // 设置请求超时时间
- }
- fmt.Println("call remote url ", uri, " data ", data)
- var count int
- for {
- body := bytes.NewBuffer([]byte(data))
- // 创建一个新的请求
- req, err := http.NewRequest("POST", uri, body)
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Tenant", "tenant_sys_online")
- if err != nil {
- return nil, err
- }
- // 发送请求
- resp, err := client.Do(req)
- if err == nil {
- defer resp.Body.Close()
- if resp.StatusCode == http.StatusOK {
- // 请求成功
- return ioutil.ReadAll(resp.Body)
- } else {
- // 可以根据需要处理其他状态码
- fmt.Printf("请求失败,状态码: %d\n", resp.StatusCode)
- }
- } else {
- // 请求出错,记录错误信息
- fmt.Println("请求错误:", err)
- }
- if count >= times {
- break
- }
- count++
- // 等待一段时间后重试
- time.Sleep(time.Second * 1)
- fmt.Printf("尝试重发 #%d...\n", count)
- }
- return nil, fmt.Errorf("达到最大重发次数,无法完成请求")
- }
- // PostJSON post json 数据请求
- func PostJSON(uri string, obj interface{}) ([]byte, error) {
- jsonData, err := json.Marshal(obj)
- if err != nil {
- return nil, err
- }
- jsonData = bytes.Replace(jsonData, []byte("\\u003c"), []byte("<"), -1)
- jsonData = bytes.Replace(jsonData, []byte("\\u003e"), []byte(">"), -1)
- jsonData = bytes.Replace(jsonData, []byte("\\u0026"), []byte("&"), -1)
- body := bytes.NewBuffer(jsonData)
- response, err := http.Post(uri, "application/json;charset=utf-8", body)
- if err != nil {
- return nil, err
- }
- defer response.Body.Close()
- if response.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
- }
- return ioutil.ReadAll(response.Body)
- }
- // PostJSONWithRespContentType post json数据请求,且返回数据类型
- func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, error) {
- jsonData, err := json.Marshal(obj)
- if err != nil {
- return nil, "", err
- }
- jsonData = bytes.Replace(jsonData, []byte("\\u003c"), []byte("<"), -1)
- jsonData = bytes.Replace(jsonData, []byte("\\u003e"), []byte(">"), -1)
- jsonData = bytes.Replace(jsonData, []byte("\\u0026"), []byte("&"), -1)
- body := bytes.NewBuffer(jsonData)
- response, err := http.Post(uri, "application/json;charset=utf-8", body)
- if err != nil {
- return nil, "", err
- }
- defer response.Body.Close()
- if response.StatusCode != http.StatusOK {
- return nil, "", fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
- }
- responseData, err := ioutil.ReadAll(response.Body)
- contentType := response.Header.Get("Content-Type")
- return responseData, contentType, err
- }
- // PostFile 上传文件
- func PostFile(fieldname, filename, uri string) ([]byte, error) {
- fields := []MultipartFormField{
- {
- IsFile: true,
- Fieldname: fieldname,
- Filename: filename,
- },
- }
- return PostMultipartForm(fields, uri)
- }
- // MultipartFormField 保存文件或其他字段信息
- type MultipartFormField struct {
- IsFile bool
- Fieldname string
- Value []byte
- Filename string
- }
- // PostMultipartForm 上传文件或其他多个字段
- func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte, err error) {
- bodyBuf := &bytes.Buffer{}
- bodyWriter := multipart.NewWriter(bodyBuf)
- for _, field := range fields {
- if field.IsFile {
- fileWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename)
- if e != nil {
- err = fmt.Errorf("error writing to buffer , err=%v", e)
- return
- }
- fh, e := os.Open(field.Filename)
- if e != nil {
- err = fmt.Errorf("error opening file , err=%v", e)
- return
- }
- defer fh.Close()
- if _, err = io.Copy(fileWriter, fh); err != nil {
- return
- }
- } else {
- partWriter, e := bodyWriter.CreateFormField(field.Fieldname)
- if e != nil {
- err = e
- return
- }
- valueReader := bytes.NewReader(field.Value)
- if _, err = io.Copy(partWriter, valueReader); err != nil {
- return
- }
- }
- }
- contentType := bodyWriter.FormDataContentType()
- bodyWriter.Close()
- resp, e := http.Post(uri, contentType, bodyBuf)
- if e != nil {
- err = e
- return
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- return nil, err
- }
- respBody, err = ioutil.ReadAll(resp.Body)
- return
- }
- // httpWithTLS CA证书
- func httpWithTLS(rootCa, key string) (*http.Client, error) {
- var client *http.Client
- certData, err := ioutil.ReadFile(rootCa)
- if err != nil {
- return nil, fmt.Errorf("unable to find cert path=%s, error=%v", rootCa, err)
- }
- cert := pkcs12ToPem(certData, key)
- config := &tls.Config{
- Certificates: []tls.Certificate{cert},
- }
- tr := &http.Transport{
- TLSClientConfig: config,
- DisableCompression: true,
- }
- client = &http.Client{Transport: tr}
- return client, nil
- }
- // pkcs12ToPem 将Pkcs12转成Pem
- func pkcs12ToPem(p12 []byte, password string) tls.Certificate {
- blocks, err := pkcs12.ToPEM(p12, password)
- defer func() {
- if x := recover(); x != nil {
- log.Print(x)
- }
- }()
- if err != nil {
- panic(err)
- }
- var pemData []byte
- for _, b := range blocks {
- pemData = append(pemData, pem.EncodeToMemory(b)...)
- }
- cert, err := tls.X509KeyPair(pemData, pemData)
- if err != nil {
- panic(err)
- }
- return cert
- }
|