ghttp.Client

GF框架提供了强大便捷易用的HTTP客户端,由ghttp模块实现。

方法列表:
https://godoc.org/github.com/gogf/gf/net/ghttp

常用方法:

  1. type Client
  2. func NewClient() *Client
  3. func (c *Client) Get(url string, data ...interface{}) (*ClientResponse, error)
  4. func (c *Client) Put(url string, data ...interface{}) (*ClientResponse, error)
  5. func (c *Client) Post(url string, data ...interface{}) (*ClientResponse, error)
  6. func (c *Client) Delete(url string, data ...interface{}) (*ClientResponse, error)
  7. func (c *Client) Connect(url string, data ...interface{}) (*ClientResponse, error)
  8. func (c *Client) Head(url string, data ...interface{}) (*ClientResponse, error)
  9. func (c *Client) Options(url string, data ...interface{}) (*ClientResponse, error)
  10. func (c *Client) Patch(url string, data ...interface{}) (*ClientResponse, error)
  11. func (c *Client) Trace(url string, data ...interface{}) (*ClientResponse, error)
  12. func (c *Client) DoRequest(method, url string, data ...interface{}) (*ClientResponse, error)
  13. func (c *Client) GetBytes(url string, data ...interface{}) []byte
  14. func (c *Client) PutBytes(url string, data ...interface{}) []byte
  15. func (c *Client) PostBytes(url string, data ...interface{}) []byte
  16. func (c *Client) DeleteBytes(url string, data ...interface{}) []byte
  17. func (c *Client) ConnectBytes(url string, data ...interface{}) []byte
  18. func (c *Client) HeadBytes(url string, data ...interface{}) []byte
  19. func (c *Client) OptionsBytes(url string, data ...interface{}) []byte
  20. func (c *Client) PatchBytes(url string, data ...interface{}) []byte
  21. func (c *Client) TraceBytes(url string, data ...interface{}) []byte
  22. func (c *Client) DoRequestBytes(method string, url string, data ...interface{}) []byte
  23. func (c *Client) GetContent(url string, data ...interface{}) string
  24. func (c *Client) PutContent(url string, data ...interface{}) string
  25. func (c *Client) PostContent(url string, data ...interface{}) string
  26. func (c *Client) DeleteContent(url string, data ...interface{}) string
  27. func (c *Client) ConnectContent(url string, data ...interface{}) string
  28. func (c *Client) HeadContent(url string, data ...interface{}) string
  29. func (c *Client) OptionsContent(url string, data ...interface{}) string
  30. func (c *Client) PatchContent(url string, data ...interface{}) string
  31. func (c *Client) TraceContent(url string, data ...interface{}) string
  32. func (c *Client) DoRequestContent(method string, url string, data ...interface{}) string
  33. func (c *Client) SetBasicAuth(user, pass string) *Client
  34. func (c *Client) SetBrowserMode(enabled bool) *Client
  35. func (c *Client) SetContentType(contentType string) *Client
  36. func (c *Client) SetCookie(key, value string) *Client
  37. func (c *Client) SetCookieMap(m map[string]string) *Client
  38. func (c *Client) SetCtx(ctx context.Context) *Client
  39. func (c *Client) SetHeader(key, value string) *Client
  40. func (c *Client) SetHeaderMap(m map[string]string) *Client
  41. func (c *Client) SetHeaderRaw(headers string) *Client
  42. func (c *Client) SetPrefix(prefix string) *Client
  43. func (c *Client) SetRetry(retryCount int, retryInterval int) *Client
  44. func (c *Client) SetTimeout(t time.Duration) *Client

简要说明:

  1. 我们可以使用NewClient创建一个自定义的HTTP客户端对象Client,随后可以使用该对象执行请求,该对象底层使用了连接池设计,因此没有Close关闭方法。HTTP客户端对象也可以通过g.Client快捷方法创建。
  2. 客户端提供了一系列以HTTP Method命名的方法,调用这些方法将会发起对应的HTTP Method请求。常用的方法是GetPost方法,同时DoRequest是核心的请求方法,用户可以调用该方法实现自定义的HTTP Method发送请求。
  3. 请求返回结果为*ClientResponse对象,可以通过该结果对象获取对应的返回结果,通过ReadAll/ReadAllString方法可以获得返回的内容,该对象在使用完毕后需要通过Close方法关闭,防止内存溢出。
  4. *Bytes方法用于获得服务端返回的二进制数据,如果请求失败返回nil*Content方法用于请求获得字符串结果数据,如果请求失败返回空字符串;Set*方法用于Client的参数设置。
  5. 可以看到,客户端的请求参数的数据参数data数据类型为interface{}类型,也就是说可以传递任意的数据类型,常见的参数数据类型为string/map,如果参数为map类型,参数值将会被自动urlencode编码。

ghttp.ClientResponse

ClientResponse为HTTP对应请求的返回结果对象,该对象继承于http.Response,可以使用http.Response的所有方法。在此基础之上增加了以下几个方法:

  1. func (r *ClientResponse) GetCookie(key string) string
  2. func (r *ClientResponse) GetCookieMap() map[string]string
  3. func (r *ClientResponse) ReadAll() []byte
  4. func (r *ClientResponse) ReadAllString() string
  5. func (r *ClientResponse) Close()

这里也要提醒的是,ClientResponse需要手动调用Close方法关闭,也就是说,不管你使用不使用返回的ClientResponse对象,你都需要将该返回对象赋值给一个变量,并且手动调用其Close方法进行关闭(往往使用defer r.Close())。

一些重要说明

  1. ghttp客户端默认关闭了KeepAlive功能以及对服务端TLS证书的校验功能,如果需要启用可自定义客户端的Transport属性。
  2. 连接池参数设定、连接代理设置这些高级功能也可以通过自定义客户端的Transport属性实现,该数据继承于标准库的http.Transport对象。