102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
package conn
|
||
|
||
import (
|
||
"crypto/tls"
|
||
"io"
|
||
"io/ioutil"
|
||
"log"
|
||
"net/http"
|
||
"net/url"
|
||
)
|
||
|
||
// headers := map[string]string{
|
||
// "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||
// }
|
||
func DT_POST(urls string, headers map[string]string, bytess io.Reader) []byte {
|
||
url, err := url.Parse(urls)
|
||
if err != nil {
|
||
log.Println(err)
|
||
}
|
||
// 创建一个 POST 请求
|
||
r, err := http.NewRequest(http.MethodPost, url.String(), bytess)
|
||
if err != nil {
|
||
log.Println(err)
|
||
}
|
||
|
||
// // 设置请求头
|
||
// req.Header.Set("Content-Type", "application/json")
|
||
// 使用 map 动态设置请求头 // 设置请求头 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||
|
||
for key, value := range headers {
|
||
r.Header.Set(key, value) // 使用 Set 方法设置请求头
|
||
}
|
||
// 创建一个 HTTP 客户端,跳过证书验证
|
||
tr := &http.Transport{
|
||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||
}
|
||
// 发送请求
|
||
client := &http.Client{Transport: tr}
|
||
resp, err := client.Do(r)
|
||
if err != nil {
|
||
log.Println(err)
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
// 检查响应状态码
|
||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||
log.Printf("server returned: %s", resp.Status)
|
||
}
|
||
|
||
// 读取响应体内容
|
||
body, err := ioutil.ReadAll(resp.Body)
|
||
if err != nil {
|
||
log.Println(err)
|
||
}
|
||
// log.Println(string(body))
|
||
// var gcresp map[string]interface{}
|
||
// if err := ScanJson(resp, gcresp); err != nil {
|
||
// log.Println(err)
|
||
// }
|
||
|
||
return body
|
||
}
|
||
|
||
// req 请求体 url 请求地址 headers请求头
|
||
func DT_GET(req map[string]interface{}, urls string) map[string]interface{} {
|
||
url, err := url.Parse(urls)
|
||
if err != nil {
|
||
log.Println(err)
|
||
}
|
||
body, err := ToJsonBuff(req)
|
||
if err != nil {
|
||
log.Println()
|
||
}
|
||
log.Println("hson请求体:", body)
|
||
r, err := http.NewRequest(http.MethodGet, url.String(), body)
|
||
if err != nil {
|
||
log.Println()
|
||
}
|
||
// 使用 map 动态设置请求头
|
||
headers := map[string]string{
|
||
"Content-Type": "application/json",
|
||
"Authorization": "Bearer YOUR_ACCESS_TOKEN",
|
||
"Accept-Language": "en-US",
|
||
"cookie": "__ac_nonce=0638733a400869171be51",
|
||
}
|
||
for key, value := range headers {
|
||
r.Header.Set(key, value) // 使用 Set 方法设置请求头
|
||
}
|
||
resp, err := http.DefaultClient.Do(r)
|
||
if err != nil {
|
||
log.Println(err)
|
||
}
|
||
if resp.StatusCode != http.StatusOK {
|
||
log.Printf("响应失败,状态码为 %s", resp.Status)
|
||
}
|
||
var gcresp map[string]interface{}
|
||
if err := ScanJson(resp, gcresp); err != nil {
|
||
log.Println(err)
|
||
}
|
||
return gcresp
|
||
}
|