Commit 179a4ecc by zhengqiuyun86

数组差集算法

parent d7645d43
package httpclient
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
// HttpClient 本项目中使用的http客户端
var HttpClient *http.Client
func init() {
HttpClient = &http.Client{}
//HttpClient.Timeout = config.Conf.Http.Client.Timeout * time.Millisecond //设置请求超时时间
}
// Get get方法的http请求,返回原始字符串
func Get(url string) ([]byte, error) {
rep, err := HttpClient.Get(url)
if err != nil {
return nil, err
}
defer rep.Body.Close()
data, err := ioutil.ReadAll(rep.Body)
if err != nil {
return nil, err
}
return data, nil
}
// GetWithHeader get方法的http请求,返回原始字符串
func GetWithHeader(url string, header *map[string]string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
//设置header
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
if header != nil {
for k, v := range *header {
req.Header.Set(k, v)
}
}
//发送请求
rep, err := HttpClient.Do(req)
if err != nil {
return nil, err
}
defer rep.Body.Close()
//处理返回值
data, err := ioutil.ReadAll(rep.Body)
if err != nil {
return nil, err
}
return data, nil
}
// Post post请求,json
// url 请求地址
// header header参数
// badyParam body参数
func Post(url string, header *map[string]string, badyParam *interface{}) ([]byte, error) {
//处理body参数
var bt []byte
if badyParam != nil {
b, err := json.Marshal(*badyParam)
if err != nil {
return nil, err
}
bt = b
}
//创建post请求
req, err := http.NewRequest("POST", url, bytes.NewReader(bt))
if err != nil {
return nil, err
}
//设置header
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
if header != nil {
for k, v := range *header {
req.Header.Set(k, v)
}
}
//发送请求
rep, err := HttpClient.Do(req)
if err != nil {
return nil, err
}
defer rep.Body.Close()
//处理返回值
data, err := ioutil.ReadAll(rep.Body)
if err != nil {
return nil, err
}
return data, nil
}
...@@ -31,3 +31,13 @@ func TestPostJSONStr(t *testing.T) { ...@@ -31,3 +31,13 @@ func TestPostJSONStr(t *testing.T) {
} }
fmt.Printf("请求结果:%s\n", string(responseData)) fmt.Printf("请求结果:%s\n", string(responseData))
} }
func TestGet(t *testing.T) {
responseData, err := Get("https://test-go-rym-oxygen-manage-service.168cad.top/oxygen/u/price/rule/detail/info?id=1", http.Client{
Timeout: 10 * time.Second,
})
if err != nil {
fmt.Printf("错误:%s\n", err.Error())
}
fmt.Printf("请求结果:%s\n", string(responseData))
}
...@@ -3,7 +3,7 @@ package util ...@@ -3,7 +3,7 @@ package util
import ( import (
"bytes" "bytes"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io/ioutil" "io"
"net/http" "net/http"
"strings" "strings"
) )
...@@ -21,6 +21,19 @@ const ( ...@@ -21,6 +21,19 @@ const (
XmlForm = "application/x-www-form-urlencoded;charset=utf-8" XmlForm = "application/x-www-form-urlencoded;charset=utf-8"
) )
func Get(url string, client http.Client) ([]byte, error) {
rep, err := client.Get(url)
if err != nil {
return nil, err
}
defer rep.Body.Close()
data, err := io.ReadAll(rep.Body)
if err != nil {
return nil, err
}
return data, nil
}
func PostFormStr(url string, params string, header map[string]string, client http.Client) ([]byte, error) { func PostFormStr(url string, params string, header map[string]string, client http.Client) ([]byte, error) {
p := StrToUrlValue(params) p := StrToUrlValue(params)
requestStr := p.Encode() requestStr := p.Encode()
...@@ -41,7 +54,7 @@ func PostFormStr(url string, params string, header map[string]string, client htt ...@@ -41,7 +54,7 @@ func PostFormStr(url string, params string, header map[string]string, client htt
return nil, err return nil, err
} }
defer response.Body.Close() defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body) data, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -66,7 +79,7 @@ func PostJSONStr(url string, params string, header map[string]string, client htt ...@@ -66,7 +79,7 @@ func PostJSONStr(url string, params string, header map[string]string, client htt
return nil, err return nil, err
} }
defer response.Body.Close() defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body) data, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment