httpclient

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: BSD-3-Clause Imports: 9 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Client added in v0.2.4

func Client() *http.Client

Client :获取当前 client

func Get added in v0.2.4

func Get(ctx context.Context, rawURL string, header map[string]string, cookies []*http.Cookie) ([]byte, error)

Get :基础用法

func GetWithQuery added in v0.2.4

func GetWithQuery(
	ctx context.Context,
	rawURL string,
	query map[string]string,
	header map[string]string,
	cookies []*http.Cookie,
) ([]byte, error)

GetWithQuery :支持 query 参数

func Post

func Post(url string, body []byte) ([]byte, error)

Post :基础用法

func PostWithHeader added in v0.2.4

func PostWithHeader(
	url string,
	body []byte,
	header map[string]string,
) ([]byte, error)

PostWithHeader :支持 header

func PostWithOptions added in v0.2.4

func PostWithOptions(
	ctx context.Context,
	url string,
	body []byte,
	header map[string]string,
	cookies []*http.Cookie,
) ([]byte, error)

PostWithOptions :完整参数

func SetConfig added in v0.2.4

func SetConfig(c *Config)

SetConfig :整体替换(推荐用于启动阶段)

func UpdateConfig added in v0.2.4

func UpdateConfig(fn func(*Config))

UpdateConfig :按需修改(线程安全)

Types

type Config added in v0.2.4

type Config struct {

	// Timeout 指整个 HTTP 请求的超时时间。
	// 包含:连接、重定向、读取响应体的总耗时。
	// 建议:对第三方 API 设置较小值(如 3~5 秒), 对内部服务可适当放宽。
	Timeout time.Duration

	// MaxIdleConns 控制所有主机的最大空闲连接数。
	// 影响全局连接池大小。 默认值过小会导致频繁建连,过大会占用过多资源。
	MaxIdleConns int

	// MaxIdleConnsPerHost 控制每个 Host 的最大空闲连接数。
	// 在高并发访问同一服务时非常重要。
	// 如果你访问的是单一 API 服务,这个值应该适当调大。
	MaxIdleConnsPerHost int

	// IdleConnTimeout 指空闲连接在连接池中保持的最长时间。
	// 超过该时间未使用的连接会被关闭。
	// 合理设置可以避免长期占用无用连接。
	IdleConnTimeout time.Duration

	// DisableCompression 控制是否禁用自动解压响应体。
	// 默认 false,表示支持 gzip/deflate。
	// 在调试抓包或对接不规范服务时可能需要禁用。
	DisableCompression bool

	// TLSConfig 用于自定义 TLS 行为(HTTPS)。
	// 常见用途:
	//   - 设置最低 TLS 版本
	//   - 指定根证书(自签名证书)
	//   - 测试环境下跳过证书校验(不推荐用于生产)
	TLSConfig *tls.Config

	// Proxy 指定 HTTP 代理函数。
	// 可用于:
	//   - 公司内网代理
	//   - 调试流量(Charles / Fiddler)
	//   - 特定请求走代理
	// 若为 nil,则不使用代理。
	Proxy func(*http.Request) (*url.URL, error)
}

Config 定义 httpclient 的全局配置项。 一般在程序启动阶段设置,运行中修改会触发 Transport 重建。

func DefaultConfig added in v0.2.4

func DefaultConfig() *Config

Jump to

Keyboard shortcuts

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