Documentation
¶
Index ¶
- Constants
- Variables
- func CheckHTTPStatus(response *Response) error
- func IsConnectionRefused(err error) bool
- func IsDNSError(err error) bool
- func IsInvalidAddressError(err error) bool
- func IsNetworkUnreachable(err error) bool
- func IsRetryableError(err error) bool
- func IsTimeoutError(err error) bool
- func ToString(value interface{}) string
- type BusinessError
- type HTTPStatusError
- type HttpClient
- func (h *HttpClient) AddHeader(name, value string)
- func (h *HttpClient) Close()
- func (h *HttpClient) DialWebSocket(ctx context.Context, rawURL string, options WebSocketOptions) (*WebSocketConn, *http.Response, error)
- func (h *HttpClient) DisableJA3()
- func (h *HttpClient) Do(ctx context.Context, method, rawURL string, body io.Reader, ...) (*Response, error)
- func (h *HttpClient) DoDelete(path string, body ...map[string]string) ([]byte, error)
- func (h *HttpClient) DoDeleteRaw(path, rawBody string) ([]byte, error)
- func (h *HttpClient) DoGet(path string) ([]byte, error)
- func (h *HttpClient) DoGetRaw(path string) ([]byte, error)
- func (h *HttpClient) DoGetRawWithHeader(path string) ([]byte, http.Header, error)
- func (h *HttpClient) DoGetRedirect(path string) (string, error)
- func (h *HttpClient) DoGetRedirectWithSession(s *Session, path string) (string, error)
- func (h *HttpClient) DoGetWithSession(s *Session, path string) ([]byte, error)
- func (h *HttpClient) DoHead(path string) (http.Header, error)
- func (h *HttpClient) DoOptions(path string) (http.Header, error)
- func (h *HttpClient) DoPatch(path string, patchData map[string]string) ([]byte, error)
- func (h *HttpClient) DoPatchAny(path string, patchData interface{}) ([]byte, error)
- func (h *HttpClient) DoPatchRaw(path, rawBody string) ([]byte, error)
- func (h *HttpClient) DoPost(path string, postData map[string]string) ([]byte, error)
- func (h *HttpClient) DoPostAny(path string, postData interface{}) ([]byte, error)
- func (h *HttpClient) DoPostMultipart(path string, fields map[string]string) ([]byte, error)
- func (h *HttpClient) DoPostRaw(path, rawBody string) ([]byte, error)
- func (h *HttpClient) DoPostWithSession(s *Session, path string, postData map[string]string) ([]byte, error)
- func (h *HttpClient) DoPut(path string, putData map[string]string) ([]byte, error)
- func (h *HttpClient) DoPutRaw(path string, raw []byte) ([]byte, error)
- func (h *HttpClient) DownloadFile(path, savePath string) error
- func (h *HttpClient) EnableJA3(profile string) error
- func (h *HttpClient) GetCookieValue(name string) string
- func (h *HttpClient) GetCookieValueFor(rawURL, name string) string
- func (h *HttpClient) GetCookies() []*http.Cookie
- func (h *HttpClient) GetCookiesFor(rawURL string) []*http.Cookie
- func (h *HttpClient) GetDomain() string
- func (h *HttpClient) GetHeader() map[string]string
- func (h *HttpClient) GetHeaderValue(name string) string
- func (h *HttpClient) GetTimeout() time.Duration
- func (h *HttpClient) IsLoggerEnabled() bool
- func (h *HttpClient) LogError(msg string, err error)
- func (h *HttpClient) LogInfo(msg string, fields ...interface{})
- func (h *HttpClient) NewOperation(domain string, session *Session) *HttpClient
- func (h *HttpClient) RemoveHeader(name string)
- func (h *HttpClient) Session() *Session
- func (h *HttpClient) SetCookie(name, value string)
- func (h *HttpClient) SetCookies(cookies map[string]string, opts ...bool)
- func (h *HttpClient) SetCookiesFor(rawURL string, cookies map[string]string, opts ...bool)
- func (h *HttpClient) SetDomain(domain string)
- func (h *HttpClient) SetHeader(headers map[string]string)
- func (h *HttpClient) SetLogger(logger *zap.SugaredLogger)
- func (h *HttpClient) SetProxy(cfg *ProxyConfig) error
- func (h *HttpClient) SetResponseChecker(checker ResponseChecker)
- func (h *HttpClient) SetTimeout(timeout time.Duration)
- func (h *HttpClient) UploadFile(path, fieldName, filePath string, extraParams map[string]string) ([]byte, error)
- type ProxyConfig
- type RequestOptions
- type Response
- type ResponseChecker
- type Session
- type TransportConfig
- type WebSocketConn
- type WebSocketMessageType
- type WebSocketOptions
- type WebSocketStatusCode
Constants ¶
const ( WebSocketMessageText = websocket.MessageText WebSocketMessageBinary = websocket.MessageBinary WebSocketStatusNormalClosure = websocket.StatusNormalClosure WebSocketStatusGoingAway = websocket.StatusGoingAway )
Variables ¶
var ErrSessionExpired = errors.New("session expired")
ErrSessionExpired 表示业务认证已失效,由业务响应检查器显式标记。 本库不会根据某个业务码或 HTTP 401 自动认定会话失效。
Functions ¶
func CheckHTTPStatus ¶ added in v1.1.5
CheckHTTPStatus 是可选的响应检查器,仅接受 2xx。 NoRedirect 下需要处理 3xx 的调用方应提供自己的检查器。
func IsConnectionRefused ¶ added in v1.1.0
func IsDNSError ¶ added in v1.1.0
func IsInvalidAddressError ¶ added in v1.1.0
func IsNetworkUnreachable ¶ added in v1.1.0
func IsRetryableError ¶ added in v1.1.0
IsRetryableError 判断是否为可重试的瞬态连接错误。 适用场景:高并发下代理/服务端提前关闭连接、TLS 握手 EOF 等。
func IsTimeoutError ¶ added in v1.1.0
Types ¶
type BusinessError ¶ added in v1.1.5
BusinessError 保留远端业务码和消息;Cause 可用于标记 ErrSessionExpired 等错误类别。 错误中不要放入 Token、Cookie 或包含授权码的 URL;Message 来自远端,记录前需自行脱敏。
func (*BusinessError) Error ¶ added in v1.1.5
func (e *BusinessError) Error() string
func (*BusinessError) Unwrap ¶ added in v1.1.5
func (e *BusinessError) Unwrap() error
type HTTPStatusError ¶ added in v1.1.5
type HTTPStatusError struct {
StatusCode int
}
HTTPStatusError 表示未通过 HTTP 状态检查,不包含可能泄露凭证的正文和 URL。 原始响应仍通过 Do 的第一个返回值获取。
func (*HTTPStatusError) Error ¶ added in v1.1.5
func (e *HTTPStatusError) Error() string
type HttpClient ¶
type HttpClient struct {
// contains filtered or unexported fields
}
HttpClient 封装了 http.Client,提供连接池、代理、JA3 指纹、并发限制等功能。
func NewHttpClient ¶
func NewHttpClient(domain string, timeout ...time.Duration) *HttpClient
NewHttpClient 使用默认传输配置创建 HttpClient。
func NewHttpClientWithTransport ¶ added in v1.1.0
func NewHttpClientWithTransport(domain string, tc *TransportConfig, timeout ...time.Duration) *HttpClient
NewHttpClientWithTransport 使用自定义传输配置创建 HttpClient。
func (*HttpClient) AddHeader ¶ added in v1.0.13
func (h *HttpClient) AddHeader(name, value string)
AddHeader 添加或覆盖单个请求头。
func (*HttpClient) DialWebSocket ¶ added in v1.1.5
func (h *HttpClient) DialWebSocket(ctx context.Context, rawURL string, options WebSocketOptions) (*WebSocketConn, *http.Response, error)
DialWebSocket 使用完整 ws/wss URL 建立连接,共享 HTTP Transport 和代理配置。 Header 优先级:公共默认值 < Session < 当前连接;空值列表删除同名项。 不自动重试或跟随重定向。ctx 只限制握手,连接存活不受握手超时影响。 返回的响应供检查握手状态与 Header,调用方无需关闭其 Body。 HttpClient.Close 不关闭此连接,调用方应调用 Close 或 CloseNow。
func (*HttpClient) DisableJA3 ¶ added in v1.0.13
func (h *HttpClient) DisableJA3()
DisableJA3 关闭 JA3 指纹模拟,恢复默认 TLS。
func (*HttpClient) Do ¶ added in v1.1.4
func (h *HttpClient) Do(ctx context.Context, method, rawURL string, body io.Reader, options RequestOptions) (*Response, error)
Do 使用完整 HTTP(S) URL 执行请求,不修改默认域名、Header 或 Cookie。 不自动重试,避免重复提交;ctx 覆盖排队、连接及响应读取阶段。 Transport、代理、JA3 和默认超时必须在并发请求开始前配置完成。 默认不检查 HTTP/业务状态;配置检查器后,检查失败同时返回完整 Response 和 error。 网络/解码/读取失败时不运行检查器;读取失败可能返回包含部分正文的 Response。
func (*HttpClient) DoDeleteRaw ¶ added in v1.1.0
func (h *HttpClient) DoDeleteRaw(path, rawBody string) ([]byte, error)
DoDeleteRaw 发送带原始字符串 body 的 DELETE 请求。
func (*HttpClient) DoGetRaw ¶ added in v1.0.19
func (h *HttpClient) DoGetRaw(path string) ([]byte, error)
DoGetRaw 发送 GET 请求(带详细日志)。
func (*HttpClient) DoGetRawWithHeader ¶ added in v1.1.4
DoGetRawWithHeader 发送 GET 请求并返回响应头(带详细日志)。 用于下载类接口读取响应头(如 Content-Disposition 的 fileName)。
func (*HttpClient) DoGetRedirect ¶ added in v1.1.2
func (h *HttpClient) DoGetRedirect(path string) (string, error)
DoGetRedirect 发送 GET 请求并阻止跟随重定向,返回 3xx 响应的 Location 跳转链接。 若响应非 3xx 或没有 Location 头,返回空字符串。
func (*HttpClient) DoGetRedirectWithSession ¶ added in v1.1.2
func (h *HttpClient) DoGetRedirectWithSession(s *Session, path string) (string, error)
DoGetRedirectWithSession 使用独立 Session 发送 GET 请求并阻止跟随重定向,返回 Location 跳转链接。
func (*HttpClient) DoGetWithSession ¶ added in v1.1.0
func (h *HttpClient) DoGetWithSession(s *Session, path string) ([]byte, error)
DoGetWithSession 使用独立 Session(独立 CookieJar)发送 GET 请求。
func (*HttpClient) DoHead ¶ added in v1.1.0
func (h *HttpClient) DoHead(path string) (http.Header, error)
DoHead 发送 HEAD 请求,返回响应 Headers(body 始终为空)。
func (*HttpClient) DoOptions ¶ added in v1.1.0
func (h *HttpClient) DoOptions(path string) (http.Header, error)
DoOptions 发送 OPTIONS 请求,返回响应 Headers(含 Allow 等协商字段)。
func (*HttpClient) DoPatch ¶ added in v1.1.0
DoPatch 发送 PATCH 请求,根据 Content-Type 自动序列化(JSON / form)。
func (*HttpClient) DoPatchAny ¶ added in v1.1.0
func (h *HttpClient) DoPatchAny(path string, patchData interface{}) ([]byte, error)
DoPatchAny 发送 PATCH 请求,body 支持任意结构体(仅 JSON)。
func (*HttpClient) DoPatchRaw ¶ added in v1.1.0
func (h *HttpClient) DoPatchRaw(path, rawBody string) ([]byte, error)
DoPatchRaw 发送带原始字符串 body 的 PATCH 请求。
func (*HttpClient) DoPostAny ¶ added in v1.0.6
func (h *HttpClient) DoPostAny(path string, postData interface{}) ([]byte, error)
DoPostAny 发送 POST 请求,body 支持任意结构体(仅 JSON)。
func (*HttpClient) DoPostMultipart ¶ added in v1.0.19
DoPostMultipart 发送 multipart/form-data 格式的 POST 请求(纯字段,无文件)。
func (*HttpClient) DoPostRaw ¶ added in v1.0.6
func (h *HttpClient) DoPostRaw(path, rawBody string) ([]byte, error)
DoPostRaw 发送带原始字符串 body 的 POST 请求。
func (*HttpClient) DoPostWithSession ¶ added in v1.1.0
func (h *HttpClient) DoPostWithSession(s *Session, path string, postData map[string]string) ([]byte, error)
DoPostWithSession 使用独立 Session(独立 CookieJar)发送 POST 请求。
func (*HttpClient) DoPutRaw ¶ added in v1.0.16
func (h *HttpClient) DoPutRaw(path string, raw []byte) ([]byte, error)
DoPutRaw 发送原始二进制数据(适用于 OSS 上传等场景)。
func (*HttpClient) DownloadFile ¶ added in v1.0.13
func (h *HttpClient) DownloadFile(path, savePath string) error
DownloadFile 下载远程文件并保存到本地 savePath。
func (*HttpClient) EnableJA3 ¶ added in v1.0.11
func (h *HttpClient) EnableJA3(profile string) error
EnableJA3 仅允许在发送请求前配置。 开启 JA3 TLS 指纹模拟;profile 为空时等同于 DisableJA3。 支持:chrome、firefox、safari、edge、ios。
func (*HttpClient) GetCookieValue ¶ added in v1.0.13
func (h *HttpClient) GetCookieValue(name string) string
GetCookieValue 返回默认域名下指定名称 Cookie 的值。
func (*HttpClient) GetCookieValueFor ¶ added in v1.1.0
func (h *HttpClient) GetCookieValueFor(rawURL, name string) string
GetCookieValueFor 获取指定 URL 域名下某个 Cookie 的值(多域名场景)。
func (*HttpClient) GetCookies ¶
func (h *HttpClient) GetCookies() []*http.Cookie
GetCookies 返回默认域名下的所有 Cookie。
func (*HttpClient) GetCookiesFor ¶ added in v1.1.0
func (h *HttpClient) GetCookiesFor(rawURL string) []*http.Cookie
GetCookiesFor 获取指定 URL 域名下的所有 Cookie(多域名场景)。
func (*HttpClient) GetHeader ¶
func (h *HttpClient) GetHeader() map[string]string
GetHeader 返回当前所有请求头的副本(线程安全)。
func (*HttpClient) GetHeaderValue ¶ added in v1.1.4
func (h *HttpClient) GetHeaderValue(name string) string
func (*HttpClient) GetTimeout ¶ added in v1.0.4
func (h *HttpClient) GetTimeout() time.Duration
GetTimeout 返回当前请求超时时间。
func (*HttpClient) IsLoggerEnabled ¶ added in v1.1.1
func (h *HttpClient) IsLoggerEnabled() bool
IsLoggerEnabled 返回是否已设置日志记录器。
func (*HttpClient) LogError ¶ added in v1.0.1
func (h *HttpClient) LogError(msg string, err error)
LogError 输出 Error 级别日志。
func (*HttpClient) LogInfo ¶ added in v1.0.1
func (h *HttpClient) LogInfo(msg string, fields ...interface{})
LogInfo 输出 Info 级别日志。
func (*HttpClient) NewOperation ¶ added in v1.1.4
func (h *HttpClient) NewOperation(domain string, session *Session) *HttpClient
NewOperation 创建独立操作状态,复用连接池、限流器和启动阶段配置。 不复制默认 Cookie;调用方可传入已有 Session 延续登录会话。 同一个操作内允许顺序设置域名和 Header,不应并发修改操作状态。
func (*HttpClient) RemoveHeader ¶ added in v1.1.3
func (h *HttpClient) RemoveHeader(name string)
RemoveHeader 删除指定请求头。
func (*HttpClient) Session ¶ added in v1.1.4
func (h *HttpClient) Session() *Session
Session 返回当前操作的显式 Cookie 会话;默认客户端返回 Jar 快照。 不复制默认 Header,当前请求应显式提供 Header 快照。
func (*HttpClient) SetCookie ¶ added in v1.1.1
func (h *HttpClient) SetCookie(name, value string)
SetCookie 设置默认域名下的单个 Cookie。
func (*HttpClient) SetCookies ¶
func (h *HttpClient) SetCookies(cookies map[string]string, opts ...bool)
SetCookies 设置默认域名下的 Cookie;reset=true 时重新创建 CookieJar,彻底清除已有 Cookie。
func (*HttpClient) SetCookiesFor ¶ added in v1.1.0
func (h *HttpClient) SetCookiesFor(rawURL string, cookies map[string]string, opts ...bool)
SetCookiesFor 设置指定 URL 域名下的 Cookie(多域名场景);reset=true 时重新创建 CookieJar。
func (*HttpClient) SetHeader ¶
func (h *HttpClient) SetHeader(headers map[string]string)
SetHeader 批量设置请求头(已存在的 key 会被覆盖)。
func (*HttpClient) SetLogger ¶ added in v1.0.1
func (h *HttpClient) SetLogger(logger *zap.SugaredLogger)
SetLogger 设置日志记录器。
func (*HttpClient) SetProxy ¶
func (h *HttpClient) SetProxy(cfg *ProxyConfig) error
SetProxy 仅允许在发送请求前配置,运行期间不可修改共享 Transport。 配置代理(nil 表示清除代理)。支持 "http" 和 "socks5" 两种类型。
func (*HttpClient) SetResponseChecker ¶ added in v1.1.5
func (h *HttpClient) SetResponseChecker(checker ResponseChecker)
SetResponseChecker 设置 Do 的默认响应检查器;nil 清除配置。 并发安全,已开始的请求继续使用开始时的检查器快照。 NewOperation 会继承创建操作时的配置,之后两者独立。
func (*HttpClient) SetTimeout ¶ added in v1.0.4
func (h *HttpClient) SetTimeout(timeout time.Duration)
SetTimeout 在发送请求前设置默认超时;单次请求超时请使用 Do 的 context。
func (*HttpClient) UploadFile ¶
func (h *HttpClient) UploadFile(path, fieldName, filePath string, extraParams map[string]string) ([]byte, error)
UploadFile 通过 multipart/form-data 上传本地文件。 fieldName 为文件字段名,filePath 为本地路径,extraParams 为附加表单字段。
type ProxyConfig ¶
ProxyConfig 代理配置。
type RequestOptions ¶ added in v1.1.4
type RequestOptions struct {
Headers http.Header
Session *Session
NoRedirect bool
// ResponseChecker 非 nil 时覆盖客户端默认检查器。
ResponseChecker ResponseChecker
// SkipResponseCheck 跳过所有响应检查,优先于 ResponseChecker。
SkipResponseCheck bool
}
RequestOptions 是单次请求配置;调用期间不要修改 Headers。 Session 为空时不自动携带或保存 Cookie。NoRedirect 禁止自动跟随跳转。
type ResponseChecker ¶ added in v1.1.5
ResponseChecker 检查已完整读取的最终响应(NoRedirect 时为当前跳转响应)。 返回 nil 表示通过;检查器不得修改响应,并应支持并发调用。 检查器仅由 Do 调用,不影响旧 DoGet/DoPost 等方法。
type Session ¶ added in v1.1.0
type Session struct {
// contains filtered or unexported fields
}
Session 代表一个独立的 HTTP 会话,拥有独立的 CookieJar。 适用于多账号/多用户并发场景,各 goroutine 持有各自的 Session。
func (*Session) GetCookieValue ¶ added in v1.1.0
GetCookieValue 获取指定 URL 域名下某个 Cookie 的值。
func (*Session) GetCookies ¶ added in v1.1.0
GetCookies 获取指定 URL 域名下的所有 Cookie。
func (*Session) SetCookies ¶ added in v1.1.0
SetCookies 设置指定 URL 域名下的 Cookie。 reset=true 时重新创建 CookieJar,彻底清除所有已有 Cookie。
type TransportConfig ¶ added in v1.1.0
type TransportConfig struct {
MaxIdleConns int
MaxIdleConnsPerHost int
MaxConnsPerHost int
IdleConnTimeout time.Duration
MaxConcurrency int // 最大并发请求数,0 表示不限制
}
TransportConfig HTTP 传输层配置。
type WebSocketConn ¶ added in v1.1.5
WebSocketConn 支持 Context 读写、Ping、Subprotocol 和关闭握手。 调用方负责关闭连接;读写 Context 超时或取消会关闭连接。
type WebSocketMessageType ¶ added in v1.1.5
type WebSocketMessageType = websocket.MessageType
type WebSocketOptions ¶ added in v1.1.5
type WebSocketOptions struct {
Headers http.Header
Session *Session
Subprotocols []string
HandshakeTimeout time.Duration // 0 使用客户端默认超时;包含并发排队时间
MaxMessageSize int64 // 0 使用默认 1 MiB;必须非负
EnableCompression bool // 默认关闭;启用时不使用压缩上下文复用
}
WebSocketOptions 是单次连接配置,调用期间不要修改 Headers 或 Subprotocols。 Session 为空时不自动携带或保存 Cookie;仍可显式指定 Cookie Header。
type WebSocketStatusCode ¶ added in v1.1.5
type WebSocketStatusCode = websocket.StatusCode
func WebSocketCloseStatus ¶ added in v1.1.5
func WebSocketCloseStatus(err error) WebSocketStatusCode
WebSocketCloseStatus 提取关闭码,非关闭错误返回 -1。