You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.3 KiB
Go
101 lines
2.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
func Md5(str string) string {
|
|
hash := md5.New()
|
|
hash.Write([]byte(str))
|
|
bytes := hash.Sum(nil)
|
|
md5Str := hex.EncodeToString(bytes)
|
|
return md5Str
|
|
}
|
|
|
|
func Sign(params url.Values, signKey string) string {
|
|
fmt.Println(params.Encode() + "&signkey=" + signKey)
|
|
return Md5(params.Encode() + "&signkey=" + signKey)
|
|
}
|
|
|
|
func Post(url string, params url.Values) {
|
|
client := &http.Client{}
|
|
body := strings.NewReader(params.Encode())
|
|
request, err := http.NewRequest("POST", url, body)
|
|
request.Header.Set("content-type", "application/x-www-form-urlencoded")
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
response, err := client.Do(request)
|
|
|
|
defer func(Body io.ReadCloser) {
|
|
err := Body.Close()
|
|
if err != nil {
|
|
|
|
}
|
|
}(response.Body)
|
|
|
|
bodyBytes, err := io.ReadAll(response.Body)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(string(bodyBytes))
|
|
}
|
|
|
|
func Post1(url string, params url.Values) {
|
|
client := &http.Client{}
|
|
//reqBodyBytes, _ := json.Marshal(params)
|
|
//body := strings.NewReader(string(reqBodyBytes))
|
|
body := strings.NewReader(params.Encode())
|
|
request, err := http.NewRequest("POST", url, body)
|
|
request.SetBasicAuth("AUOi0TCR3m9_LGyxPGo15X2Nvvd6D-l8WTgTBMj39XqyzuFTwTPT3zxU11o0tWT8ouGZ4kFQv2mtmK7f", "EI7JYZQUOl4emunnuOMhCbqKFVFMxcO4Qk9ZctlrUu4h1Q_Gv_i-YDMhHv5Rt2J74lbEEPiuHb8Ot6eo")
|
|
request.Header.Set("content-type", "application/json")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
response, err := client.Do(request)
|
|
defer func(Body io.ReadCloser) {
|
|
err := Body.Close()
|
|
if err != nil {
|
|
|
|
}
|
|
}(response.Body)
|
|
|
|
bodyBytes, err := io.ReadAll(response.Body)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(string(bodyBytes))
|
|
}
|
|
|
|
func Post2(url string, params string, token string) {
|
|
client := &http.Client{}
|
|
body := strings.NewReader(params)
|
|
request, err := http.NewRequest("POST", url, body)
|
|
request.Header.Set("Content-Type", "application/json")
|
|
request.Header.Set("Accept", "application/json")
|
|
request.Header.Set("Authorization", "Bearer "+token)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
response, err := client.Do(request)
|
|
defer func(Body io.ReadCloser) {
|
|
err := Body.Close()
|
|
if err != nil {
|
|
|
|
}
|
|
}(response.Body)
|
|
|
|
bodyBytes, err := io.ReadAll(response.Body)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(string(bodyBytes))
|
|
}
|