Documentation ¶
Overview ¶
Package http @Title client.go @Description @Author haogooder @Update 2022/2/8
Package http @Title response.go @Description @Author haogooder @Update 2022/2/8
Package http @Title utils.go @Description @Author haogooder @Update 2022/2/8
Index ¶
- func HttpBuildQuery(queryData url.Values) string
- func JoinRawCookie(ck map[string]string) (ret string)
- func ParseUrl(str string, component int) (map[string]string, error)
- func ProcTime(st, et int64) float64
- func RawUrlDecode(str string) (string, error)
- func RawUrlEncode(str string) string
- func SplitRawCookie(ck string) (ret map[string]string)
- func UrlDecode(str string) (string, error)
- func UrlEncode(str string) string
- type HttpClient
- func (ehc *HttpClient) AddCookie(k, v string) *HttpClient
- func (ehc *HttpClient) AddCookies(ck map[string]string) *HttpClient
- func (ehc *HttpClient) AddField(k, v string) *HttpClient
- func (ehc *HttpClient) AddFile(fieldName, filePath, fileName string) *HttpClient
- func (ehc *HttpClient) AddHeader(k string, v string) *HttpClient
- func (ehc *HttpClient) AddHeaders(hs map[string]string) *HttpClient
- func (ehc *HttpClient) Get() (*HttpResponse, error)
- func (ehc *HttpClient) GetBuffer() *bytes.Buffer
- func (ehc *HttpClient) GetContentType() string
- func (ehc *HttpClient) GetHttpTransport() *http.Transport
- func (ehc *HttpClient) Head() (*HttpResponse, error)
- func (ehc *HttpClient) Post() (*HttpResponse, error)
- func (ehc *HttpClient) PostForm() (*HttpResponse, error)
- func (ehc *HttpClient) SetAjax() *HttpClient
- func (ehc *HttpClient) SetBasicAuth(username, password string) *HttpClient
- func (ehc *HttpClient) SetCheckRedirectFunc(policy func(req *http.Request, via []*http.Request) error) *HttpClient
- func (ehc *HttpClient) SetContentType(ct string) *HttpClient
- func (ehc *HttpClient) SetContentTypeFormUrlEncoded() *HttpClient
- func (ehc *HttpClient) SetContentTypeJson() *HttpClient
- func (ehc *HttpClient) SetContentTypeOctetStream() *HttpClient
- func (ehc *HttpClient) SetCtx(ctx context.Context) *HttpClient
- func (ehc *HttpClient) SetField(k, v string) *HttpClient
- func (ehc *HttpClient) SetFields(data map[string]string) *HttpClient
- func (ehc *HttpClient) SetHeader(k string, v string) *HttpClient
- func (ehc *HttpClient) SetHeaders(hs map[string]string) *HttpClient
- func (ehc *HttpClient) SetHost(host string) *HttpClient
- func (ehc *HttpClient) SetKeepAlive(b bool) *HttpClient
- func (ehc *HttpClient) SetProxy(proxyHost string) *HttpClient
- func (ehc *HttpClient) SetRawCookie(ck string) *HttpClient
- func (ehc *HttpClient) SetRawRequestBody(b []byte) *HttpClient
- func (ehc *HttpClient) SetReferer(referer string) *HttpClient
- func (ehc *HttpClient) SetRetryTimes(t int) *HttpClient
- func (ehc *HttpClient) SetTimeout(t time.Duration) *HttpClient
- func (ehc *HttpClient) SetUrl(u string) *HttpClient
- func (ehc *HttpClient) SetUserAgent(ua string) *HttpClient
- type HttpResponse
- func (tfr HttpResponse) Error() error
- func (tfr HttpResponse) GetBody() []byte
- func (tfr HttpResponse) GetBodyString() string
- func (tfr HttpResponse) GetContentLen() int64
- func (tfr HttpResponse) GetHeader() map[string]string
- func (tfr HttpResponse) GetLocation() string
- func (tfr HttpResponse) GetProto() string
- func (tfr HttpResponse) GetSetCookie() []http.Cookie
- func (tfr HttpResponse) GetStatus() string
- func (tfr HttpResponse) GetStatusCode() int
- func (tfr HttpResponse) GetTransferEncoding() []string
- type HttpUploadFile
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type HttpClient ¶
type HttpClient struct {
// contains filtered or unexported fields
}
请求结构体
func NewHttpClient ¶
func NewHttpClient(httpUrl string, ctx context.Context) *HttpClient
NewHttpClient 构造一个请求结构体
func (*HttpClient) AddCookie ¶
func (ehc *HttpClient) AddCookie(k, v string) *HttpClient
添加单个cookie的键值
func (*HttpClient) AddCookies ¶
func (ehc *HttpClient) AddCookies(ck map[string]string) *HttpClient
批量添加cookie的键值
func (*HttpClient) AddField ¶
func (ehc *HttpClient) AddField(k, v string) *HttpClient
添加单个字段,可以添加数组,如 : client.AddField("a","1") 、client.AddField("a","2") 在接收的时候将收到 a=[1,2]
func (*HttpClient) AddFile ¶
func (ehc *HttpClient) AddFile(fieldName, filePath, fileName string) *HttpClient
添加要上传的文件
func (*HttpClient) AddHeader ¶
func (ehc *HttpClient) AddHeader(k string, v string) *HttpClient
添加单条header信息
func (*HttpClient) AddHeaders ¶
func (ehc *HttpClient) AddHeaders(hs map[string]string) *HttpClient
批量设置头信息
func (*HttpClient) GetBuffer ¶
func (ehc *HttpClient) GetBuffer() *bytes.Buffer
获取buf地址,直接操作即可。此为POST 操作里的 body 如传递raw post的body信息时:httpClient.GetBuffer().Write(c)
func (*HttpClient) GetHttpTransport ¶
func (ehc *HttpClient) GetHttpTransport() *http.Transport
提取http.Transport进行二次设置
func (*HttpClient) Post ¶
func (ehc *HttpClient) Post() (*HttpResponse, error)
发起POST请求并返回数据,有字段、上传文件,或者raw post用的 一般,raw post body 不会和上传文件、其他from表单信息同时出现
func (*HttpClient) PostForm ¶
func (ehc *HttpClient) PostForm() (*HttpResponse, error)
发起POST请求并返回数据,没有上传文件,只是简单的模拟提交表单操作
func (*HttpClient) SetBasicAuth ¶
func (ehc *HttpClient) SetBasicAuth(username, password string) *HttpClient
设置HTTP Basic Authentication the provided username and password(设置header的相应值)
func (*HttpClient) SetCheckRedirectFunc ¶
func (ehc *HttpClient) SetCheckRedirectFunc(policy func(req *http.Request, via []*http.Request) error) *HttpClient
设置跳转策略
func (*HttpClient) SetContentType ¶
func (ehc *HttpClient) SetContentType(ct string) *HttpClient
设置内容类型(设置header的相应值)
func (*HttpClient) SetContentTypeFormUrlEncoded ¶
func (ehc *HttpClient) SetContentTypeFormUrlEncoded() *HttpClient
设置表单内容类型(设置header的相应值)
func (*HttpClient) SetContentTypeJson ¶
func (ehc *HttpClient) SetContentTypeJson() *HttpClient
设置json内容类型(设置header的相应值)
func (*HttpClient) SetContentTypeOctetStream ¶
func (ehc *HttpClient) SetContentTypeOctetStream() *HttpClient
设置二进制流内容类型(设置header的相应值)
func (*HttpClient) SetCtx ¶
func (ehc *HttpClient) SetCtx(ctx context.Context) *HttpClient
设置context
func (*HttpClient) SetFields ¶
func (ehc *HttpClient) SetFields(data map[string]string) *HttpClient
批量添加字段,一般是 PostForm 用,在即上传文件又有字段时Post也用
func (*HttpClient) SetHeader ¶
func (ehc *HttpClient) SetHeader(k string, v string) *HttpClient
添加单条header信息
func (*HttpClient) SetHeaders ¶
func (ehc *HttpClient) SetHeaders(hs map[string]string) *HttpClient
批量设置头信息
func (*HttpClient) SetHost ¶
func (ehc *HttpClient) SetHost(host string) *HttpClient
设置要请求的host(设置header的相应值)
func (*HttpClient) SetProxy ¶
func (ehc *HttpClient) SetProxy(proxyHost string) *HttpClient
设置代理用的地址和端口
func (*HttpClient) SetRawCookie ¶
func (ehc *HttpClient) SetRawCookie(ck string) *HttpClient
设置cookie(设置header的相应值)
func (*HttpClient) SetRawRequestBody ¶
func (ehc *HttpClient) SetRawRequestBody(b []byte) *HttpClient
设置原始的请求的body信息,像请求Apollo的接口时候就让提交json字符串,没有别的参数 一般只有POST/PUT用得着,等同于httpClient.GetBuffer().Write(c)
func (*HttpClient) SetReferer ¶
func (ehc *HttpClient) SetReferer(referer string) *HttpClient
设置referer(设置header的相应值)
func (*HttpClient) SetRetryTimes ¶
func (ehc *HttpClient) SetRetryTimes(t int) *HttpClient
设置重试次数,默认3次
func (*HttpClient) SetTimeout ¶
func (ehc *HttpClient) SetTimeout(t time.Duration) *HttpClient
设置整体超时时间,默认3秒
func (*HttpClient) SetUserAgent ¶
func (ehc *HttpClient) SetUserAgent(ua string) *HttpClient
设置userAgent(设置header的相应值)
type HttpResponse ¶
type HttpResponse struct {
// contains filtered or unexported fields
}
响应结构体,在response基础上封装
func NewHttpResponse ¶
func NewHttpResponse(resp *http.Response) *HttpResponse
func (HttpResponse) GetSetCookie ¶
func (tfr HttpResponse) GetSetCookie() []http.Cookie
获取响应头里面set-cookie设置cookie信息的列表
func (HttpResponse) GetTransferEncoding ¶
func (tfr HttpResponse) GetTransferEncoding() []string
提取响应的编码信息
type HttpUploadFile ¶
type HttpUploadFile struct { FieldName string // 上传文件时用的字段名称 FilePath string // 文件的绝对路径 FileName string // 上传时显示的文件名称,如果为空则取filePath的basename }
上传文件的设置