ghttp.Client

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

简要说明:

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

ghttp.ClientResponse

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

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

一些重要说明

  1. ghttp客户端默认关闭了KeepAlive功能以及对服务端TLS证书的校验功能,如果需要启用可自定义客户端的Transport属性。