ghttp.Client
方法列表: https://godoc.org/github.com/gogf/gf/net/ghttp
简要说明:
- 我们可以使用
NewClient
创建一个自定义的HTTP客户端对象Client
,随后可以使用该对象执行请求,该对象底层使用了连接池设计,因此没有Close
关闭方法。HTTP客户端对象也可以通过g.Client
快捷方法创建。 - 客户端提供了一系列以
HTTP Method
命名的方法,调用这些方法将会发起对应的HTTP Method
请求。常用的方法是Get
和Post
方法,同时DoRequest
是核心的请求方法,用户可以调用该方法实现自定义的HTTP Method
发送请求。 - 请求返回结果为
*ClientResponse
对象,可以通过该结果对象获取对应的返回结果,通过ReadAll
/ReadAllString
方法可以获得返回的内容,该对象在使用完毕后需要通过方法关闭,防止内存溢出。 - 可以看到,客户端的请求参数的数据参数
data
数据类型为interface{}
类型,也就是说可以传递任意的数据类型,常见的参数数据类型为string
/map
,如果参数为map
类型,参数值将会被自动urlencode
编码。
ghttp.ClientResponse
func (r *ClientResponse) GetCookie(key string) string
func (r *ClientResponse) GetCookieMap() map[string]string
func (r *ClientResponse) RawDump()
func (r *ClientResponse) RawResponse() string
func (r *ClientResponse) ReadAll() []byte
func (r *ClientResponse) ReadAllString() string
func (r *ClientResponse) Close() error
这里也要提醒的是,ClientResponse
需要手动调用Close
方法关闭,也就是说,不管你使用不使用返回的ClientResponse
对象,你都需要将该返回对象赋值给一个变量,并且手动调用其Close
方法进行关闭(往往使用defer r.Close()
)。
一些重要说明
ghttp
客户端默认关闭了KeepAlive
功能以及对服务端TLS
证书的校验功能,如果需要启用可自定义客户端的Transport
属性。