http

package
v1.0.3-Realize Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

* 包http包含了一个高度集成的http客户端

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConf

func NewConf() *conf

构建一个配置实例

Types

type CallBack

type CallBack func(result *Result, err error)

异步回调。

type Corps

type Corps struct {
	// contains filtered or unexported fields
}

支持并发使用的Http客户端

支持同步和异步请求

func New

func New(config ...*conf) *Corps

构建一个Http客户端

允许传入配置信息,当传入多个的情况下仅接受第一个

func (*Corps) Do

func (slf *Corps) Do(method Method, url string) *Req

构建请求

type Method

type Method string

请求方法结构描述

const (
	Get  Method = "GET"
	Post Method = "POST"
)

type Req

type Req struct {
	Err error // 请求是否存在异常
	// contains filtered or unexported fields
}

构建完毕待发出的请求结构描述

在Req中可以针对请求需要的内容进行调整。

在设定完毕后使用SynchronousExec或AsynchronousExec函数即可发出异步或同步请求。

func (*Req) AddContentTypeParam

func (slf *Req) AddContentTypeParam(param string) *Req

添加请求Content-Type参数。

func (*Req) AddFormParam

func (slf *Req) AddFormParam(key, value string) *Req

表单中添加参数

func (*Req) AddFormParamBunch

func (slf *Req) AddFormParamBunch(bunch string) *Req

表单中添加参数串

func (*Req) AddUrlParam

func (slf *Req) AddUrlParam(key, value string) *Req

请求URL中添加参数

func (*Req) AddUrlParamBunch

func (slf *Req) AddUrlParamBunch(bunch string) *Req

请求URL中添加参数串

func (*Req) AsynchronousExec

func (slf *Req) AsynchronousExec(callback CallBack)

发起异步请求。

func (*Req) Content

func (slf *Req) Content(content string, contentType string)

设置请求发送特定类型的文本信息。

func (*Req) CreateFormFile

func (slf *Req) CreateFormFile(fieldName string, filePath string) *Req

表单中写入一个文件

func (*Req) DisableCompression

func (slf *Req) DisableCompression(disable bool) *Req

设置自动压缩 如果DisableCompression为真,会禁止Transport在请求中没有Accept-Encoding头时, 主动添加"Accept-Encoding: gzip"头,以获取压缩数据。

如果Transport自己请求gzip并得到了压缩后的回复,它会主动解压缩回复的主体。

但如果用户显式的请求gzip压缩数据,Transport是不会主动解压缩的。

func (*Req) Json

func (slf *Req) Json(json []byte)

设置请求发送JSON数据。

func (*Req) JsonEntity

func (slf *Req) JsonEntity(jsonEntity interface{}) *Req

设置请求发送JSON数据。

func (*Req) ResetCookie

func (slf *Req) ResetCookie() *Req

清空所有附加的Cookie信息

func (*Req) SetContentType

func (slf *Req) SetContentType(contentType string) *Req

设置请求Content-Type。

func (*Req) SetCookie

func (slf *Req) SetCookie(cookie *http.Cookie) *Req

设置请求Cookie。

通过SetCookie设置的Cookie信息并非是一次性的。

在设置过Cookie后,之后的每一次发起请求都会为Crops附加上这些Cookie信息, 如果需要清空Cookie信息请使用“ResetCookie”函数

func (*Req) SetHeader

func (slf *Req) SetHeader(name, value string) *Req

设置请求临时Header

这样设置的Header仅在本次请求中生效。

func (*Req) SetHeaderForever

func (slf *Req) SetHeaderForever(name, value string) *Req

设置请求永久Header

这样设置的Header将会在整个Corps实例中均存在。

func (*Req) SetProxy

func (slf *Req) SetProxy(proxyHandler func(*http.Request) (*url.URL, error))

设置以代理的方式来进行请求

例:return url.Parse("socks5://127.0.0.1:1080")

func (*Req) SkipSecureVerify

func (slf *Req) SkipSecureVerify(skip bool) *Req

跳过安全证书验证

将会影响整个Corps接下来的所有的请求。

func (*Req) SynchronousExec

func (slf *Req) SynchronousExec() (*Result, error)

发起同步请求。

type Result

type Result struct {
	// contains filtered or unexported fields
}

对HTTP客户端请求结果的结构描述

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL