直接上代码,想知道每行什么意思就自己百度去,工具类不详细说明
package httpReq
import (
"bytes"
"compress/flate"
"compress/gzip"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/util/grand"
"io"
"net/http"
"net/url"
"strings"
)
// httpReq
type httpReq struct {
headerMap map[string]string
proxyIp []string
}
// HttpReq 方便调用
func HttpReq() *httpReq {
return &httpReq{}
}
// Get 发起Get请求
func (obj *httpReq) Get(urlS string, data ...map[string]string) (string, error) {
if len(data) > 0 {
separate := "?"
if strings.Index(urlS, "?") != -1 {
separate = "&"
}
for k, v := range data[0] {
urlS += separate + k + "=" + v
if separate == "?" {
separate = "&"
}
}
}
request, err := http.NewRequest("GET", urlS, nil)
if err != nil {
return "", err
}
return obj.httpRequest(request)
}
// Post 发起Post请求
func (obj *httpReq) Post(urlPath string, bodyMap map[string]string) (string, error) {
if obj.contentType() == "json" {
return obj.postJson(urlPath, bodyMap)
}
return obj.postForm(urlPath, bodyMap)
}
// postForm 表单形式发起post请求
func (obj *httpReq) postForm(urlPath string, bodyMap map[string]string) (string, error) {
dataVal := url.Values{}
// 拼接参数
if bodyMap != nil {
for k, v := range bodyMap {
dataVal.Set(k, v)
}
}
obj.headerMap["Content-Type"] = "application/x-www-form-urlencoded"
request, requestErr := http.NewRequest("POST", urlPath, strings.NewReader(dataVal.Encode()))
if requestErr != nil {
return "", nil
}
return obj.httpRequest(request)
}
// postJson json形式发起post请求
func (obj *httpReq) postJson(urlPath string, data ...map[string]string) (string, error) {
var buffer *bytes.Buffer
if len(data) > 0 {
jsData, err := gjson.New(data[0]).ToJson()
if err != nil {
return "", err
}
buffer = bytes.NewBuffer(jsData)
} else {
buffer = bytes.NewBuffer([]byte(""))
}
obj.headerMap["Content-Type"] = "application/json"
request, requestErr := http.NewRequest("POST", urlPath, buffer)
if requestErr != nil {
return "", nil
}
return obj.httpRequest(request)
}
// SetHeader 设置Header头
func (obj *httpReq) SetHeader(data map[string]string) {
if _, ok := data["Content-Type"]; !ok {
data["Content-Type"] = "application/x-www-form-urlencoded"
}
obj.headerMap = data
}
// httpRequest 执行请求
func (obj *httpReq) httpRequest(request *http.Request) (string, error) {
// 设置请求头
for k, v := range obj.headerMap {
request.Header.Add(k, v)
}
client := &http.Client{}
//是否使用代理IP
if len(obj.proxyIp) > 0 {
rand := grand.N(1, len(obj.proxyIp))
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(obj.proxyIp[rand])
}
transport := &http.Transport{Proxy: proxy}
client = &http.Client{Transport: transport}
} else {
client = &http.Client{}
}
httpResponse, getErr := client.Do(request)
if getErr != nil {
return "", getErr
}
resp, zErr := obj.switchContentEncoding(httpResponse)
if zErr != nil {
return "", zErr
}
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(resp)
return buf.String(), err
}
func (obj *httpReq) switchContentEncoding(res *http.Response) (bodyReader io.Reader, err error) {
switch res.Header.Get("Content-Encoding") {
case "gzip":
bodyReader, err = gzip.NewReader(res.Body)
case "deflate":
bodyReader = flate.NewReader(res.Body)
default:
bodyReader = res.Body
}
return
}
func (obj *httpReq) contentType() string {
reqType := "form"
ct := "application/x-www-form-urlencoded"
if _, ok := obj.headerMap["Content-Type"]; ok {
ct = obj.headerMap["Content-Type"]
}
if ct == "application/json" {
reqType = "json"
}
return reqType
}
使用方式
url := "http://www....."
// 获取对象
reqObj := httpReq.HttpReq()
// 数组header头里的User-Agent 其他的也这样设置
reqObj.SetHeader(map[string]string{
"User-Agent": ua,
})
// 发起get请求并获取结果
res, _ := reqObj.Get(url)
// 发起post请求并获取结果
res, _ := reqObj.Post(url, map[string]string{
"id": "10",
})
评论