gf/g/net/ghttp/ghttp_client_request.go

218 lines
6.0 KiB
Go
Raw Normal View History

2017-12-29 16:03:30 +08:00
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// 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,
// You can obtain one at https://gitee.com/johng/gf.
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 (
"time"
2017-12-07 14:57:16 +08:00
"bytes"
2018-01-10 18:20:32 +08:00
"strings"
"net/http"
"mime/multipart"
"os"
"io"
2017-11-23 10:21:28 +08:00
)
2017-12-07 14:57:16 +08:00
// http客户端
type Client struct {
http.Client // 底层http client对象
header map[string]string // header
authUser string // HTTP基本权限设置名称
authPass string // HTTP基本权限设置密码
2017-12-07 14:57:16 +08:00
}
2017-11-23 10:21:28 +08:00
// http客户端对象指针
func NewClient() (*Client) {
2018-05-28 17:02:53 +08:00
return &Client{
Client : http.Client {
2018-05-28 17:02:53 +08:00
Transport: &http.Transport {
DisableKeepAlives: true,
},
},
header : make(map[string]string),
2018-05-28 17:02:53 +08:00
}
2017-11-23 10:21:28 +08:00
}
// 设置HTTP Headerss
func (c *Client) SetHeader(key, value string) {
c.header[key] = value
}
2017-11-23 10:21:28 +08:00
// 设置请求过期时间
func (c *Client) SetTimeOut(t time.Duration) {
c.Timeout = t
}
// 设置HTTP访问账号密码
func (c *Client) SetBasicAuth(user, pass string) {
c.authUser = user
c.authPass = pass
}
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) {
return c.DoRequest("GET", url, []byte(""))
2017-11-23 10:21:28 +08:00
}
// PUT请求
2017-12-07 14:57:16 +08:00
func (c *Client) Put(url, data string) (*ClientResponse, error) {
return c.DoRequest("PUT", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
// POST请求提交数据
2018-01-10 18:20:32 +08:00
// 支持文件上传需要字段格式为FieldName=@file:
2017-12-07 14:57:16 +08:00
func (c *Client) Post(url, data string) (*ClientResponse, error) {
var req *http.Request
if strings.Contains(data, "@file:") {
buffer := new(bytes.Buffer)
writer := multipart.NewWriter(buffer)
for _, item := range strings.Split(data, "&") {
array := strings.Split(item, "=")
if len(array[1]) > 6 && strings.Compare(array[1][0:6], "@file:") == 0 {
if file, err := writer.CreateFormFile(array[0], array[1][6:]); err == nil {
if f, err := os.Open(array[1][6:]); err == nil {
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 {
writer.WriteField(array[0], array[1])
2018-01-10 18:20:32 +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 {
req = r
req.Header.Set("Content-Type", writer.FormDataContentType())
2018-01-10 18:20:32 +08:00
}
} else {
if r, err := http.NewRequest("POST", url, bytes.NewReader([]byte(data))); err != nil {
return nil, err
} else {
req = r
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
2018-01-10 18:20:32 +08:00
}
// 自定义header
if len(c.header) > 0 {
for k, v := range c.header {
req.Header.Set(k, v)
}
}
// HTTP账号密码
if len(c.authUser) > 0 {
req.SetBasicAuth(c.authUser, c.authPass)
}
2018-01-10 18:20:32 +08:00
// 执行请求
resp, err := c.Do(req)
2017-11-23 10:21:28 +08:00
if err != nil {
2017-12-07 14:57:16 +08:00
return nil, err
2017-11-23 10:21:28 +08:00
}
r := &ClientResponse{}
r.Response = *resp
2017-12-07 14:57:16 +08:00
return r, nil
2017-11-23 10:21:28 +08:00
}
// DELETE请求
2017-12-07 14:57:16 +08:00
func (c *Client) Delete(url, data string) (*ClientResponse, error) {
return c.DoRequest("DELETE", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func (c *Client) Head(url, data string) (*ClientResponse, error) {
return c.DoRequest("HEAD", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func (c *Client) Patch(url, data string) (*ClientResponse, error) {
return c.DoRequest("PATCH", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func (c *Client) Connect(url, data string) (*ClientResponse, error) {
return c.DoRequest("CONNECT", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func (c *Client) Options(url, data string) (*ClientResponse, error) {
return c.DoRequest("OPTIONS", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func (c *Client) Trace(url, data string) (*ClientResponse, error) {
return c.DoRequest("TRACE", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
// 请求并返回response对象该方法支持二进制提交数据
func (c *Client) DoRequest(method, url string, data []byte) (*ClientResponse, error) {
if strings.Compare("POST", strings.ToUpper(method)) == 0 {
return c.Post(url, string(data))
}
2017-12-07 14:57:16 +08:00
req, err := http.NewRequest(strings.ToUpper(method), url, bytes.NewReader(data))
2017-11-23 10:21:28 +08:00
if err != nil {
2017-12-07 14:57:16 +08:00
return nil, err
2017-11-23 10:21:28 +08:00
}
// 自定义header
if len(c.header) > 0 {
for k, v := range c.header {
req.Header.Set(k, v)
}
}
// 执行请求
2017-11-23 10:21:28 +08:00
resp, err := c.Do(req)
if err != nil {
2017-12-07 14:57:16 +08:00
return nil, err
2017-11-23 10:21:28 +08:00
}
r := &ClientResponse{}
r.Response = *resp
2017-12-07 14:57:16 +08:00
return r, nil
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func Get(url string) (*ClientResponse, error) {
return DoRequest("GET", url, []byte(""))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func Put(url, data string) (*ClientResponse, error) {
return DoRequest("PUT", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func Post(url, data string) (*ClientResponse, error) {
2018-01-10 18:20:32 +08:00
return DoRequest("POST", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func Delete(url, data string) (*ClientResponse, error) {
return DoRequest("DELETE", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func Head(url, data string) (*ClientResponse, error) {
return DoRequest("HEAD", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func Patch(url, data string) (*ClientResponse, error) {
return DoRequest("PATCH", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func Connect(url, data string) (*ClientResponse, error) {
return DoRequest("CONNECT", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func Options(url, data string) (*ClientResponse, error) {
return DoRequest("OPTIONS", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
func Trace(url, data string) (*ClientResponse, error) {
return DoRequest("TRACE", url, []byte(data))
2017-11-23 10:21:28 +08:00
}
2017-12-07 14:57:16 +08:00
// 该方法支持二进制提交数据
func DoRequest(method, url string, data []byte) (*ClientResponse, error) {
return NewClient().DoRequest(method, url, data)
2017-11-23 10:21:28 +08:00
}