parent
7c13b8e2c9
commit
9c6861a559
@ -1,281 +0,0 @@
|
||||
package gohttp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go.dtapp.net/library/utils/goheader"
|
||||
"go.dtapp.net/library/utils/gorequest"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
Status string
|
||||
StatusCode int
|
||||
Header http.Header
|
||||
Body []byte
|
||||
ContentLength int64
|
||||
}
|
||||
|
||||
func Get(url string, params map[string]interface{}) (httpResponse Response, err error) {
|
||||
// 创建 http 客户端
|
||||
client := &http.Client{}
|
||||
// 创建请求
|
||||
req, _ := http.NewRequest(http.MethodGet, url, nil)
|
||||
if len(params) > 0 {
|
||||
// GET 请求携带查询参数
|
||||
q := req.URL.Query()
|
||||
for k, v := range params {
|
||||
q.Add(k, getString(v))
|
||||
}
|
||||
req.URL.RawQuery = q.Encode()
|
||||
}
|
||||
// 设置请求头
|
||||
req.Header.Set("User-Agent", gorequest.GetRandomUserAgent())
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
// 格式化返回错误
|
||||
return httpResponse, errors.New(fmt.Sprintf("请求出错 %s", err))
|
||||
}
|
||||
// 最后关闭连接
|
||||
defer resp.Body.Close()
|
||||
// 读取内容
|
||||
respBody, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return httpResponse, errors.New(fmt.Sprintf("解析内容出错 %s", err))
|
||||
}
|
||||
httpResponse.Status = resp.Status
|
||||
httpResponse.StatusCode = resp.StatusCode
|
||||
httpResponse.Header = resp.Header
|
||||
httpResponse.Body = respBody
|
||||
httpResponse.ContentLength = resp.ContentLength
|
||||
return httpResponse, err
|
||||
}
|
||||
|
||||
func GetJsonHeader(url string, params map[string]interface{}, headers goheader.Headers) (httpResponse Response, err error) {
|
||||
// 创建 http 客户端
|
||||
client := &http.Client{}
|
||||
// 创建请求
|
||||
req, _ := http.NewRequest(http.MethodGet, url, nil)
|
||||
if len(params) > 0 {
|
||||
// GET 请求携带查询参数
|
||||
q := req.URL.Query()
|
||||
for k, v := range params {
|
||||
q.Add(k, getString(v))
|
||||
}
|
||||
req.URL.RawQuery = q.Encode()
|
||||
}
|
||||
// 设置请求头
|
||||
req.Header.Set("User-Agent", gorequest.GetRandomUserAgent())
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
for key, value := range headers {
|
||||
req.Header.Set(key, value.(string))
|
||||
}
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
// 格式化返回错误
|
||||
return httpResponse, errors.New(fmt.Sprintf("请求出错 %s", err))
|
||||
}
|
||||
// 最后关闭连接
|
||||
defer resp.Body.Close()
|
||||
// 读取内容
|
||||
respBody, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return httpResponse, errors.New(fmt.Sprintf("解析内容出错 %s", err))
|
||||
}
|
||||
httpResponse.Status = resp.Status
|
||||
httpResponse.StatusCode = resp.StatusCode
|
||||
httpResponse.Header = resp.Header
|
||||
httpResponse.Body = respBody
|
||||
httpResponse.ContentLength = resp.ContentLength
|
||||
return httpResponse, err
|
||||
}
|
||||
|
||||
func PostForm(targetUrl string, params map[string]interface{}) (httpResponse Response, err error) {
|
||||
// 创建 http 客户端
|
||||
client := &http.Client{}
|
||||
// 携带 form 参数
|
||||
form := url.Values{}
|
||||
if len(params) > 0 {
|
||||
for k, v := range params {
|
||||
form.Add(k, getString(v))
|
||||
}
|
||||
}
|
||||
// 创建请求
|
||||
req, _ := http.NewRequest(http.MethodPost, targetUrl, strings.NewReader(form.Encode()))
|
||||
// 设置请求头
|
||||
req.Header.Set("User-Agent", gorequest.GetRandomUserAgent())
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
// 格式化返回错误
|
||||
return httpResponse, errors.New(fmt.Sprintf("请求出错 %s", err))
|
||||
}
|
||||
// 最后关闭连接
|
||||
defer resp.Body.Close()
|
||||
// 读取内容
|
||||
respBody, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return httpResponse, errors.New(fmt.Sprintf("解析内容出错 %s", err))
|
||||
}
|
||||
httpResponse.Status = resp.Status
|
||||
httpResponse.StatusCode = resp.StatusCode
|
||||
httpResponse.Header = resp.Header
|
||||
httpResponse.Body = respBody
|
||||
httpResponse.ContentLength = resp.ContentLength
|
||||
return httpResponse, err
|
||||
}
|
||||
|
||||
func PostJson(targetUrl string, paramsStr []byte) (httpResponse Response, err error) {
|
||||
// 创建请求
|
||||
req, _ := http.NewRequest(http.MethodPost, targetUrl, bytes.NewBuffer(paramsStr))
|
||||
// 设置请求头
|
||||
req.Header.Set("User-Agent", gorequest.GetRandomUserAgent())
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
// 创建 http 客户端
|
||||
client := &http.Client{}
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
// 格式化返回错误
|
||||
return httpResponse, errors.New(fmt.Sprintf("请求出错 %s", err))
|
||||
}
|
||||
// 最后关闭连接
|
||||
defer resp.Body.Close()
|
||||
// 读取内容
|
||||
respBody, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return httpResponse, errors.New(fmt.Sprintf("解析内容出错 %s", err))
|
||||
}
|
||||
httpResponse.Status = resp.Status
|
||||
httpResponse.StatusCode = resp.StatusCode
|
||||
httpResponse.Header = resp.Header
|
||||
httpResponse.Body = respBody
|
||||
httpResponse.ContentLength = resp.ContentLength
|
||||
return httpResponse, err
|
||||
}
|
||||
|
||||
func PostXml(targetUrl string, paramsStr []byte) (httpResponse Response, err error) {
|
||||
// 创建请求
|
||||
req, _ := http.NewRequest(http.MethodPost, targetUrl, bytes.NewReader(paramsStr))
|
||||
// 设置请求头
|
||||
req.Header.Set("User-Agent", gorequest.GetRandomUserAgent())
|
||||
// 创建 http 客户端
|
||||
client := &http.Client{}
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
// 格式化返回错误
|
||||
return httpResponse, errors.New(fmt.Sprintf("请求出错 %s", err))
|
||||
}
|
||||
// 最后关闭连接
|
||||
defer resp.Body.Close()
|
||||
// 读取内容
|
||||
respBody, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return httpResponse, errors.New(fmt.Sprintf("解析内容出错 %s", err))
|
||||
}
|
||||
httpResponse.Status = resp.Status
|
||||
httpResponse.StatusCode = resp.StatusCode
|
||||
httpResponse.Header = resp.Header
|
||||
httpResponse.Body = respBody
|
||||
httpResponse.ContentLength = resp.ContentLength
|
||||
return httpResponse, err
|
||||
}
|
||||
|
||||
func PostJsonHeader(targetUrl string, paramsStr []byte, headers goheader.Headers) (httpResponse Response, err error) {
|
||||
// 创建请求
|
||||
req, _ := http.NewRequest(http.MethodPost, targetUrl, bytes.NewBuffer(paramsStr))
|
||||
// 设置请求头
|
||||
req.Header.Set("User-Agent", gorequest.GetRandomUserAgent())
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
for key, value := range headers {
|
||||
req.Header.Set(key, value.(string))
|
||||
}
|
||||
// 创建 http 客户端
|
||||
client := &http.Client{}
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
// 格式化返回错误
|
||||
return httpResponse, errors.New(fmt.Sprintf("请求出错 %s", err))
|
||||
}
|
||||
// 最后关闭连接
|
||||
defer resp.Body.Close()
|
||||
// 读取内容
|
||||
respBody, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return httpResponse, errors.New(fmt.Sprintf("解析内容出错 %s", err))
|
||||
}
|
||||
httpResponse.Status = resp.Status
|
||||
httpResponse.StatusCode = resp.StatusCode
|
||||
httpResponse.Header = resp.Header
|
||||
httpResponse.Body = respBody
|
||||
httpResponse.ContentLength = resp.ContentLength
|
||||
return httpResponse, err
|
||||
}
|
||||
|
||||
func PostCert(targetUrl string, params io.Reader, p12Cert *tls.Certificate) (httpResponse Response, err error) {
|
||||
if p12Cert == nil {
|
||||
return httpResponse, errors.New("need p12Cert")
|
||||
}
|
||||
transport := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
Certificates: []tls.Certificate{*p12Cert},
|
||||
},
|
||||
DisableCompression: true,
|
||||
}
|
||||
// 创建请求
|
||||
req, _ := http.NewRequest(http.MethodPost, targetUrl, params)
|
||||
// 设置请求头
|
||||
req.Header.Set("User-Agent", gorequest.GetRandomUserAgent())
|
||||
// 创建 http 客户端
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
// 格式化返回错误
|
||||
return httpResponse, errors.New(fmt.Sprintf("请求出错 %s", err))
|
||||
}
|
||||
// 最后关闭连接
|
||||
defer resp.Body.Close()
|
||||
// 读取内容
|
||||
respBody, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return httpResponse, errors.New(fmt.Sprintf("解析内容出错 %s", err))
|
||||
}
|
||||
httpResponse.Status = resp.Status
|
||||
httpResponse.StatusCode = resp.StatusCode
|
||||
httpResponse.Header = resp.Header
|
||||
httpResponse.Body = respBody
|
||||
httpResponse.ContentLength = resp.ContentLength
|
||||
return httpResponse, err
|
||||
}
|
||||
|
||||
func getString(i interface{}) string {
|
||||
switch v := i.(type) {
|
||||
case string:
|
||||
return v
|
||||
case []byte:
|
||||
return string(v)
|
||||
case int:
|
||||
return strconv.Itoa(v)
|
||||
case bool:
|
||||
return strconv.FormatBool(v)
|
||||
default:
|
||||
marshal, _ := json.Marshal(v)
|
||||
return string(marshal)
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package gorequest
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// Headers 头部信息
|
||||
type Headers map[string]string
|
||||
|
||||
// NewHeaders 新建头部信息
|
||||
func NewHeaders() Headers {
|
||||
P := make(Headers)
|
||||
return P
|
||||
}
|
||||
|
||||
// NewNewHeadersWith 头部信息使用
|
||||
func NewNewHeadersWith(headers ...Headers) Headers {
|
||||
p := make(Headers)
|
||||
for _, v := range headers {
|
||||
p.SetHeaders(v)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// Set 设置头部信息
|
||||
func (p Headers) Set(key, value string) {
|
||||
p[key] = value
|
||||
}
|
||||
|
||||
// SetHeaders 批量设置头部信息
|
||||
func (p Headers) SetHeaders(headers Headers) {
|
||||
for key, value := range headers {
|
||||
p[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
// GetQuery 获取头部信息
|
||||
func (p Headers) GetQuery() string {
|
||||
u := url.Values{}
|
||||
for k, v := range p {
|
||||
u.Set(k, v)
|
||||
}
|
||||
return u.Encode()
|
||||
}
|
||||
|
||||
// DeepCopy 深度复制
|
||||
func (p *Headers) DeepCopy() map[string]string {
|
||||
targetMap := make(map[string]string)
|
||||
|
||||
// 从原始复制到目标
|
||||
for key, value := range *p {
|
||||
targetMap[key] = value
|
||||
}
|
||||
|
||||
// 重新申请一个新的map
|
||||
*p = map[string]string{}
|
||||
return targetMap
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package gorequest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"go.dtapp.net/gostring"
|
||||
"log"
|
||||
)
|
||||
|
||||
// Params 参数
|
||||
type Params map[string]interface{}
|
||||
|
||||
// NewParams 新建参数
|
||||
func NewParams() Params {
|
||||
P := make(Params)
|
||||
return P
|
||||
}
|
||||
|
||||
// NewParamsWith 参数使用
|
||||
func NewParamsWith(params ...Params) Params {
|
||||
p := make(Params)
|
||||
for _, v := range params {
|
||||
p.SetParams(v)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// Set 设置参数
|
||||
func (p Params) Set(key string, value interface{}) {
|
||||
p[key] = value
|
||||
}
|
||||
|
||||
// SetParams 批量设置参数
|
||||
func (p Params) SetParams(params Params) {
|
||||
for key, value := range params {
|
||||
p[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
// GetParamsString 获取参数字符串
|
||||
func GetParamsString(src interface{}) string {
|
||||
switch src.(type) {
|
||||
case string:
|
||||
return src.(string)
|
||||
case int, int8, int32, int64:
|
||||
case uint8, uint16, uint32, uint64:
|
||||
case float32, float64:
|
||||
return gostring.ToString(src)
|
||||
}
|
||||
data, err := json.Marshal(src)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// DeepCopy 深度复制
|
||||
func (p *Params) DeepCopy() map[string]interface{} {
|
||||
targetMap := make(map[string]interface{})
|
||||
|
||||
// 从原始复制到目标
|
||||
for key, value := range *p {
|
||||
targetMap[key] = value
|
||||
}
|
||||
|
||||
// 重新申请一个新的map
|
||||
*p = map[string]interface{}{}
|
||||
return targetMap
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package gorequest
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ResponseUrlParse 返回参数
|
||||
type ResponseUrlParse struct {
|
||||
Uri string `json:"uri"` // URI
|
||||
Urn string `json:"urn"` // URN
|
||||
Url string `json:"url"` // URL
|
||||
Scheme string `json:"scheme"` // 协议
|
||||
Host string `json:"host"` // 主机
|
||||
Hostname string `json:"hostname"` // 主机名
|
||||
Port string `json:"port"` // 端口
|
||||
Path string `json:"path"` // 路径
|
||||
RawQuery string `json:"raw_query"` // 参数 ?
|
||||
Fragment string `json:"fragment"` // 片段 #
|
||||
}
|
||||
|
||||
// UriParse 解析URl
|
||||
func UriParse(input string) (resp ResponseUrlParse) {
|
||||
parse, err := url.Parse(input)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
resp.Uri = input
|
||||
resp.Urn = parse.Host + parse.Path
|
||||
resp.Url = parse.Scheme + "://" + parse.Host + parse.Path
|
||||
resp.Scheme = parse.Scheme
|
||||
resp.Host = parse.Host
|
||||
resp.Hostname = parse.Hostname()
|
||||
resp.Port = parse.Port()
|
||||
resp.Path = parse.Path
|
||||
resp.RawQuery = parse.RawQuery
|
||||
resp.Fragment = parse.Fragment
|
||||
return
|
||||
}
|
||||
|
||||
// UriFilterExcludeQueryString 过滤掉url中的参数
|
||||
func UriFilterExcludeQueryString(uri string) string {
|
||||
URL, _ := url.Parse(uri)
|
||||
clearUri := strings.ReplaceAll(uri, URL.RawQuery, "")
|
||||
clearUri = strings.TrimRight(clearUri, "?")
|
||||
return strings.TrimRight(clearUri, "/")
|
||||
}
|
||||
|
||||
// LenCode 编码
|
||||
func LenCode(s string) string {
|
||||
escape := url.QueryEscape(s)
|
||||
return escape
|
||||
}
|
||||
|
||||
// DeCode 解码
|
||||
func DeCode(s string) string {
|
||||
unescape, _ := url.QueryUnescape(s)
|
||||
return unescape
|
||||
}
|
||||
|
||||
// ParseQuery 获取URL参数 https://studygolang.com/articles/2876
|
||||
func ParseQuery(s string) map[string][]string {
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
urlParam := u.RawQuery
|
||||
m, _ := url.ParseQuery(urlParam)
|
||||
return m
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package gorequest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetRandomUserAgent 获取随机UA
|
||||
func GetRandomUserAgent() string {
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
return userAgentList[r.Intn(len(userAgentList))]
|
||||
}
|
||||
|
||||
var userAgentList = []string{
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36", // Chrome 2022-02-14
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0", // Firefox 2022-02-14
|
||||
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko", // IE 2022-02-14
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.80 Safari/537.36 HeyTapBrowser/40.7.35.1", // Chrome 2022-02-14
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 OnePlusBrowser/30.5.0.8", // Chrome 2022-02-14
|
||||
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9a1) Gecko/20060814 Firefox/51.0", // Firefox 2022-02-14
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15", // Safari 2022-02-14
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15", // Safari 2022-04-18
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36 Edg/98.0.1108.51", // Edge 2022-02-14
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.44", // Chrome 2022-04-18
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36", // Chrome 2022-02-14
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36", // Chrome 2022-04-18
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0", // Firefox 2022-02-14
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0", // Firefox 2022-04-18
|
||||
}
|
||||
|
||||
// GetRandomUserAgentSystem 获取系统随机UA
|
||||
func GetRandomUserAgentSystem() string {
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
return userAgentListLinux[r.Intn(len(userAgentListLinux))]
|
||||
case "windows":
|
||||
return userAgentListWindows[r.Intn(len(userAgentListWindows))]
|
||||
default:
|
||||
return userAgentListMac[r.Intn(len(userAgentListMac))]
|
||||
}
|
||||
}
|
||||
|
||||
var userAgentListWindows = []string{
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36", // Chrome 2022-02-14
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0", // Firefox 2022-02-14
|
||||
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko", // IE 2022-02-14
|
||||
}
|
||||
|
||||
var userAgentListLinux = []string{
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.80 Safari/537.36 HeyTapBrowser/40.7.35.1", // Chrome 2022-02-14
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 OnePlusBrowser/30.5.0.8", // Chrome 2022-02-14
|
||||
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9a1) Gecko/20060814 Firefox/51.0", // Firefox 2022-02-14
|
||||
}
|
||||
|
||||
var userAgentListMac = []string{
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15", // Safari 2022-02-14
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15", // Safari 2022-04-18
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36 Edg/98.0.1108.51", // Edge 2022-02-14
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.44", // Chrome 2022-04-18
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36", // Chrome 2022-02-14
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36", // Chrome 2022-04-18
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0) Gecko/20100101 Firefox/97.0", // Firefox 2022-02-14
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0", // Firefox 2022-04-18
|
||||
}
|
||||
|
||||
func DtaUa() string {
|
||||
str := runtime.Version()
|
||||
content := str[2 : len(str)-0]
|
||||
return fmt.Sprintf("Go-dta-request/%v", content)
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package gorequest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// MarshalXML 结构体转xml
|
||||
func (p Params) MarshalXML() (reader io.Reader, err error) {
|
||||
buffer := bytes.NewBuffer(make([]byte, 0))
|
||||
|
||||
if _, err = io.WriteString(buffer, "<xml>"); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for k, v := range p {
|
||||
switch {
|
||||
case k == "detail":
|
||||
if _, err = io.WriteString(buffer, "<detail><![CDATA["); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = io.WriteString(buffer, fmt.Sprintf("%v", v)); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = io.WriteString(buffer, "]]></detail>"); err != nil {
|
||||
return
|
||||
}
|
||||
default:
|
||||
if _, err = io.WriteString(buffer, "<"+k+">"); err != nil {
|
||||
return
|
||||
}
|
||||
if err = xml.EscapeText(buffer, []byte(fmt.Sprintf("%v", v))); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = io.WriteString(buffer, "</"+k+">"); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if _, err = io.WriteString(buffer, "</xml>"); err != nil {
|
||||
return
|
||||
}
|
||||
return buffer, nil
|
||||
}
|
Loading…
Reference in new issue