http

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2022 License: MIT Imports: 20 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func HttpBuildQuery

func HttpBuildQuery(queryData url.Values) string

http_build_query()

func JoinRawCookie

func JoinRawCookie(ck map[string]string) (ret string)

合并cookie

func ParseUrl

func ParseUrl(str string, component int) (map[string]string, error)

parse_url() Parse a URL and return its components -1: all; 1: scheme; 2: host; 4: port; 8: user; 16: pass; 32: path; 64: query; 128: fragment

func ProcTime

func ProcTime(st, et int64) float64

处理请求的时间proc_time 时间要求time.Now().UnixNano()

func RawUrlDecode

func RawUrlDecode(str string) (string, error)

rawurldecode()

func RawUrlEncode

func RawUrlEncode(str string) string

高仿PHP的rawurlencode()函数,在调M端的接口时有参数要求这么处理

func SplitRawCookie

func SplitRawCookie(ck string) (ret map[string]string)

对原始cookie进行拆分

func UrlDecode

func UrlDecode(str string) (string, error)

urldecode()

func UrlEncode

func UrlEncode(str string) string

urlencode()

Types

type HttpClient

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

请求结构体

func NewEmptyHttpClient

func NewEmptyHttpClient() *HttpClient

NewEmptyHttpClient 构造空的

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) Get

func (ehc *HttpClient) Get() (*HttpResponse, error)

发起GET请求并返回数据

func (*HttpClient) GetBuffer

func (ehc *HttpClient) GetBuffer() *bytes.Buffer

获取buf地址,直接操作即可。此为POST 操作里的 body 如传递raw post的body信息时:httpClient.GetBuffer().Write(c)

func (*HttpClient) GetContentType

func (ehc *HttpClient) GetContentType() string

从header中读取内容类型

func (*HttpClient) GetHttpTransport

func (ehc *HttpClient) GetHttpTransport() *http.Transport

提取http.Transport进行二次设置

func (*HttpClient) Head

func (ehc *HttpClient) Head() (*HttpResponse, error)

发起head请求并返回数据

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) SetAjax

func (ehc *HttpClient) SetAjax() *HttpClient

设置为Ajax请求(设置header的相应值)

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) SetField

func (ehc *HttpClient) SetField(k, v string) *HttpClient

添加单个字段,POST用

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) SetKeepAlive

func (ehc *HttpClient) SetKeepAlive(b bool) *HttpClient

设置长连接选项

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) SetUrl

func (ehc *HttpClient) SetUrl(u string) *HttpClient

设置URL

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) Error

func (tfr HttpResponse) Error() error

返回出错的信息

func (HttpResponse) GetBody

func (tfr HttpResponse) GetBody() []byte

提取body信息

func (HttpResponse) GetBodyString

func (tfr HttpResponse) GetBodyString() string

返回响应的body的字符串格式

func (HttpResponse) GetContentLen

func (tfr HttpResponse) GetContentLen() int64

获取响应信息的长度

func (HttpResponse) GetHeader

func (tfr HttpResponse) GetHeader() map[string]string

提取响应的头信息

func (HttpResponse) GetLocation

func (tfr HttpResponse) GetLocation() string

获取重定向后的地址信息

func (HttpResponse) GetProto

func (tfr HttpResponse) GetProto() string

所用协议"HTTP/1.1"

func (HttpResponse) GetSetCookie

func (tfr HttpResponse) GetSetCookie() []http.Cookie

获取响应头里面set-cookie设置cookie信息的列表

func (HttpResponse) GetStatus

func (tfr HttpResponse) GetStatus() string

提取状态描述信息,如"200 OK"

func (HttpResponse) GetStatusCode

func (tfr HttpResponse) GetStatusCode() int

提取状态码,如200

func (HttpResponse) GetTransferEncoding

func (tfr HttpResponse) GetTransferEncoding() []string

提取响应的编码信息

type HttpUploadFile

type HttpUploadFile struct {
	FieldName string // 上传文件时用的字段名称
	FilePath  string // 文件的绝对路径
	FileName  string // 上传时显示的文件名称,如果为空则取filePath的basename
}

上传文件的设置

Jump to

Keyboard shortcuts

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