2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-12-30 22:02:46 +08:00
|
|
|
|
|
2018-04-13 15:19:31 +08:00
|
|
|
|
// HTTP客户端请求.
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
|
|
import (
|
2017-12-07 14:57:16 +08:00
|
|
|
|
"bytes"
|
2019-03-21 00:14:23 +08:00
|
|
|
|
"encoding/json"
|
2018-09-26 18:44:30 +08:00
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2019-03-21 00:14:23 +08:00
|
|
|
|
"github.com/gogf/gf/g/os/gfile"
|
|
|
|
|
"io"
|
|
|
|
|
"mime/multipart"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2017-11-23 10:21:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
2017-12-07 14:57:16 +08:00
|
|
|
|
// http客户端
|
|
|
|
|
type Client struct {
|
2019-03-21 00:14:23 +08:00
|
|
|
|
http.Client // 底层http client对象
|
|
|
|
|
header map[string]string // HEADER信息Map
|
|
|
|
|
cookies map[string]string // 自定义COOKIE
|
|
|
|
|
prefix string // 设置请求的URL前缀
|
|
|
|
|
authUser string // HTTP基本权限设置:名称
|
|
|
|
|
authPass string // HTTP基本权限设置:密码
|
|
|
|
|
browserMode bool // 是否模拟浏览器模式(自动保存提交COOKIE)
|
|
|
|
|
retryCount int // 失败重试次数(网络失败情况下)
|
|
|
|
|
retryInterval int // 失败重试间隔
|
2017-12-07 14:57:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// http客户端对象指针
|
2019-03-21 00:14:23 +08:00
|
|
|
|
func NewClient() *Client {
|
2018-05-28 17:02:53 +08:00
|
|
|
|
return &Client{
|
2018-07-23 11:16:21 +08:00
|
|
|
|
Client : http.Client {
|
2018-05-28 17:02:53 +08:00
|
|
|
|
Transport: &http.Transport {
|
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-03-21 00:14:23 +08:00
|
|
|
|
header : make(map[string]string),
|
|
|
|
|
cookies : make(map[string]string),
|
2018-05-28 17:02:53 +08:00
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 00:14:23 +08:00
|
|
|
|
// 克隆当前客户端对象,复制属性。
|
|
|
|
|
func (c *Client) Clone() *Client {
|
|
|
|
|
newClient := NewClient()
|
|
|
|
|
*newClient = *c
|
|
|
|
|
newClient.header = make(map[string]string)
|
|
|
|
|
newClient.cookies = make(map[string]string)
|
|
|
|
|
for k, v := range c.header {
|
|
|
|
|
newClient.header[k] = v
|
2019-01-18 22:02:17 +08:00
|
|
|
|
}
|
2019-03-21 00:14:23 +08:00
|
|
|
|
for k, v := range c.cookies {
|
|
|
|
|
newClient.cookies[k] = v
|
2019-02-26 17:17:11 +08:00
|
|
|
|
}
|
2019-03-21 00:14:23 +08:00
|
|
|
|
return newClient
|
2018-07-23 11:16:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// GET请求
|
2017-12-07 14:57:16 +08:00
|
|
|
|
func (c *Client) Get(url string) (*ClientResponse, error) {
|
2019-02-17 20:45:35 +08:00
|
|
|
|
return c.DoRequest("GET", url)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT请求
|
2019-02-17 20:45:35 +08:00
|
|
|
|
func (c *Client) Put(url string, data...string) (*ClientResponse, error) {
|
|
|
|
|
return c.DoRequest("PUT", url, data...)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-23 19:17:17 +08:00
|
|
|
|
// POST请求提交数据,默认使用表单方式提交数据(绝大部分场景下也是如此)。
|
|
|
|
|
// 如果服务端对Content-Type有要求,可使用Client对象进行请求,单独设置相关属性。
|
2018-01-10 18:20:32 +08:00
|
|
|
|
// 支持文件上传,需要字段格式为:FieldName=@file:
|
2019-02-17 20:45:35 +08:00
|
|
|
|
func (c *Client) Post(url string, data...string) (*ClientResponse, error) {
|
2018-12-31 00:50:55 +08:00
|
|
|
|
if len(c.prefix) > 0 {
|
|
|
|
|
url = c.prefix + url
|
|
|
|
|
}
|
2019-02-17 20:45:35 +08:00
|
|
|
|
param := ""
|
|
|
|
|
if len(data) > 0 {
|
|
|
|
|
param = data[0]
|
|
|
|
|
}
|
2018-12-31 00:50:55 +08:00
|
|
|
|
req := (*http.Request)(nil)
|
2019-02-17 20:45:35 +08:00
|
|
|
|
if strings.Contains(param, "@file:") {
|
2019-03-19 17:48:37 +08:00
|
|
|
|
// 文件上传
|
2018-01-11 16:06:42 +08:00
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
writer := multipart.NewWriter(buffer)
|
2019-02-17 20:45:35 +08:00
|
|
|
|
for _, item := range strings.Split(param, "&") {
|
2018-01-11 16:06:42 +08:00
|
|
|
|
array := strings.Split(item, "=")
|
|
|
|
|
if len(array[1]) > 6 && strings.Compare(array[1][0:6], "@file:") == 0 {
|
2018-09-26 18:44:30 +08:00
|
|
|
|
path := array[1][6:]
|
|
|
|
|
if !gfile.Exists(path) {
|
|
|
|
|
return nil, errors.New(fmt.Sprintf(`"%s" does not exist`, path))
|
|
|
|
|
}
|
|
|
|
|
if file, err := writer.CreateFormFile(array[0], path); err == nil {
|
|
|
|
|
if f, err := os.Open(path); err == nil {
|
2018-01-11 16:06:42 +08:00
|
|
|
|
defer f.Close()
|
|
|
|
|
if _, err = io.Copy(file, f); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-01-10 18:20:32 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-01-11 16:06:42 +08:00
|
|
|
|
writer.WriteField(array[0], array[1])
|
2018-01-10 18:20:32 +08:00
|
|
|
|
}
|
2018-01-11 16:06:42 +08:00
|
|
|
|
}
|
|
|
|
|
writer.Close()
|
|
|
|
|
if r, err := http.NewRequest("POST", url, buffer); err != nil {
|
|
|
|
|
return nil, err
|
2018-01-10 18:20:32 +08:00
|
|
|
|
} else {
|
2018-01-11 16:06:42 +08:00
|
|
|
|
req = r
|
|
|
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
2018-01-10 18:20:32 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-03-19 17:48:37 +08:00
|
|
|
|
// 识别提交数据格式
|
|
|
|
|
paramBytes := []byte(param)
|
|
|
|
|
if r, err := http.NewRequest("POST", url, bytes.NewReader(paramBytes)); err != nil {
|
2018-01-11 16:06:42 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
} else {
|
|
|
|
|
req = r
|
2019-03-19 17:48:37 +08:00
|
|
|
|
if json.Valid(paramBytes) {
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
} else {
|
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
}
|
2018-01-11 16:06:42 +08:00
|
|
|
|
}
|
2018-01-10 18:20:32 +08:00
|
|
|
|
}
|
2018-06-07 09:30:21 +08:00
|
|
|
|
// 自定义header
|
|
|
|
|
if len(c.header) > 0 {
|
|
|
|
|
for k, v := range c.header {
|
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-26 17:17:11 +08:00
|
|
|
|
// COOKIE
|
|
|
|
|
if len(c.cookies) > 0 {
|
|
|
|
|
headerCookie := ""
|
|
|
|
|
for k, v := range c.cookies {
|
|
|
|
|
if len(headerCookie) > 0 {
|
|
|
|
|
headerCookie += ";"
|
|
|
|
|
}
|
|
|
|
|
headerCookie += k + "=" + v
|
|
|
|
|
}
|
|
|
|
|
if len(headerCookie) > 0 {
|
|
|
|
|
req.Header.Set("Cookie", headerCookie)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-23 11:16:21 +08:00
|
|
|
|
// HTTP账号密码
|
|
|
|
|
if len(c.authUser) > 0 {
|
|
|
|
|
req.SetBasicAuth(c.authUser, c.authPass)
|
|
|
|
|
}
|
2018-01-10 18:20:32 +08:00
|
|
|
|
// 执行请求
|
2019-03-21 00:14:23 +08:00
|
|
|
|
resp := (*http.Response)(nil)
|
|
|
|
|
for {
|
|
|
|
|
if r, err := c.Do(req); err != nil {
|
|
|
|
|
if c.retryCount > 0 {
|
|
|
|
|
c.retryCount--
|
|
|
|
|
} else {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
resp = r
|
|
|
|
|
break
|
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2019-02-26 17:17:11 +08:00
|
|
|
|
r := &ClientResponse{
|
|
|
|
|
cookies : make(map[string]string),
|
|
|
|
|
}
|
|
|
|
|
r.Response = resp
|
2017-12-07 14:57:16 +08:00
|
|
|
|
return r, nil
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE请求
|
2019-02-17 20:45:35 +08:00
|
|
|
|
func (c *Client) Delete(url string, data...string) (*ClientResponse, error) {
|
|
|
|
|
return c.DoRequest("DELETE", url, data...)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-17 20:45:35 +08:00
|
|
|
|
func (c *Client) Head(url string, data...string) (*ClientResponse, error) {
|
|
|
|
|
return c.DoRequest("HEAD", url, data...)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-17 20:45:35 +08:00
|
|
|
|
func (c *Client) Patch(url string, data...string) (*ClientResponse, error) {
|
|
|
|
|
return c.DoRequest("PATCH", url, data...)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-17 20:45:35 +08:00
|
|
|
|
func (c *Client) Connect(url string, data...string) (*ClientResponse, error) {
|
|
|
|
|
return c.DoRequest("CONNECT", url, data...)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-17 20:45:35 +08:00
|
|
|
|
func (c *Client) Options(url string, data...string) (*ClientResponse, error) {
|
|
|
|
|
return c.DoRequest("OPTIONS", url, data...)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-17 20:45:35 +08:00
|
|
|
|
func (c *Client) Trace(url string, data...string) (*ClientResponse, error) {
|
|
|
|
|
return c.DoRequest("TRACE", url, data...)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-30 22:02:46 +08:00
|
|
|
|
// GET请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
|
2018-12-31 00:50:55 +08:00
|
|
|
|
func (c *Client) GetContent(url string, data...string) string {
|
|
|
|
|
return c.DoRequestContent("GET", url, data...)
|
2018-12-30 22:02:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
|
2018-12-31 00:50:55 +08:00
|
|
|
|
func (c *Client) PutContent(url string, data...string) string {
|
|
|
|
|
return c.DoRequestContent("PUT", url, data...)
|
2018-12-30 22:02:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
|
2018-12-31 00:50:55 +08:00
|
|
|
|
func (c *Client) PostContent(url string, data...string) string {
|
|
|
|
|
return c.DoRequestContent("POST", url, data...)
|
2018-12-30 22:02:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
|
2018-12-31 00:50:55 +08:00
|
|
|
|
func (c *Client) DeleteContent(url string, data...string) string {
|
|
|
|
|
return c.DoRequestContent("DELETE", url, data...)
|
2018-12-30 22:02:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 00:50:55 +08:00
|
|
|
|
func (c *Client) HeadContent(url string, data...string) string {
|
|
|
|
|
return c.DoRequestContent("HEAD", url, data...)
|
2018-12-30 22:02:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 00:50:55 +08:00
|
|
|
|
func (c *Client) PatchContent(url string, data...string) string {
|
|
|
|
|
return c.DoRequestContent("PATCH", url, data...)
|
2018-12-30 22:02:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 00:50:55 +08:00
|
|
|
|
func (c *Client) ConnectContent(url string, data...string) string {
|
|
|
|
|
return c.DoRequestContent("CONNECT", url, data...)
|
2018-12-30 22:02:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 00:50:55 +08:00
|
|
|
|
func (c *Client) OptionsContent(url string, data...string) string {
|
|
|
|
|
return c.DoRequestContent("OPTIONS", url, data...)
|
2018-12-30 22:02:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 00:50:55 +08:00
|
|
|
|
func (c *Client) TraceContent(url string, data...string) string {
|
|
|
|
|
return c.DoRequestContent("TRACE", url, data...)
|
2018-12-30 22:02:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
|
2018-12-31 00:50:55 +08:00
|
|
|
|
func (c *Client) DoRequestContent(method string, url string, data...string) string {
|
2019-02-17 20:45:35 +08:00
|
|
|
|
response, err := c.DoRequest(method, url, data...)
|
2018-12-30 22:02:46 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
defer response.Close()
|
|
|
|
|
return string(response.ReadAll())
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 00:50:55 +08:00
|
|
|
|
// 请求并返回response对象,该方法支持二进制提交数据
|
2019-02-17 20:45:35 +08:00
|
|
|
|
func (c *Client) DoRequest(method, url string, data...string) (*ClientResponse, error) {
|
2018-12-31 00:50:55 +08:00
|
|
|
|
if strings.EqualFold("POST", method) {
|
2019-02-17 20:45:35 +08:00
|
|
|
|
return c.Post(url, data...)
|
2018-12-31 00:50:55 +08:00
|
|
|
|
}
|
|
|
|
|
if len(c.prefix) > 0 {
|
|
|
|
|
url = c.prefix + url
|
|
|
|
|
}
|
2019-02-17 20:45:35 +08:00
|
|
|
|
param := ""
|
|
|
|
|
if len(data) > 0 {
|
|
|
|
|
param = data[0]
|
|
|
|
|
}
|
|
|
|
|
req, err := http.NewRequest(strings.ToUpper(method), url, bytes.NewReader([]byte(param)))
|
2018-12-31 00:50:55 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
// 自定义header
|
|
|
|
|
if len(c.header) > 0 {
|
|
|
|
|
for k, v := range c.header {
|
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-26 17:17:11 +08:00
|
|
|
|
// COOKIE
|
|
|
|
|
if len(c.cookies) > 0 {
|
|
|
|
|
headerCookie := ""
|
|
|
|
|
for k, v := range c.cookies {
|
|
|
|
|
if len(headerCookie) > 0 {
|
|
|
|
|
headerCookie += ";"
|
|
|
|
|
}
|
|
|
|
|
headerCookie += k + "=" + v
|
|
|
|
|
}
|
|
|
|
|
if len(headerCookie) > 0 {
|
|
|
|
|
req.Header.Set("Cookie", headerCookie)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-31 00:50:55 +08:00
|
|
|
|
// 执行请求
|
2019-03-21 00:14:23 +08:00
|
|
|
|
resp := (*http.Response)(nil)
|
|
|
|
|
for {
|
|
|
|
|
if r, err := c.Do(req); err != nil {
|
|
|
|
|
if c.retryCount > 0 {
|
|
|
|
|
c.retryCount--
|
|
|
|
|
} else {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
resp = r
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-12-31 00:50:55 +08:00
|
|
|
|
}
|
2019-02-26 17:17:11 +08:00
|
|
|
|
r := &ClientResponse{
|
|
|
|
|
cookies : make(map[string]string),
|
|
|
|
|
}
|
|
|
|
|
r.Response = resp
|
|
|
|
|
// 浏览器模式
|
|
|
|
|
if c.browserMode {
|
|
|
|
|
now := time.Now()
|
|
|
|
|
for _, v := range r.Cookies() {
|
|
|
|
|
if v.Expires.UnixNano() < now.UnixNano() {
|
|
|
|
|
delete(c.cookies, v.Name)
|
|
|
|
|
} else {
|
|
|
|
|
c.cookies[v.Name] = v.Value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//fmt.Println(url, c.cookies)
|
2018-12-31 00:50:55 +08:00
|
|
|
|
return r, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|