httpc

package module
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: Apache-2.0 Imports: 19 Imported by: 33

README

httpc

GoDoc License

Go的一个功能强大、易扩展、易使用的http客户端请求库。适合用于接口请求,模拟浏览器请求,爬虫请求。

特点

  • Cookie管理器(适合爬虫和模拟请求)
  • 支持HEADER、GET、POST、PUT、DELETE
  • 轻松上传文件下载文件
  • 支持链式调用

安装

go get github.com/Albert-Zhan/httpc

API文档

httpc在线文档

快速入门

1. 简单的请求
//新建一个请求和http客户端
req:=httpc.NewRequest(httpc.NewHttpClient())
//get请求,返回string类型的body
resp,body,err:=req.SetUrl("http://127.0.0.1").Send().End()
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(body)
}
2. 设置头信息
//新建一个http客户端
client:=httpc.NewHttpClient()
//新建一个请求
req:=httpc.NewRequest(client)
req.SetMethod("post").SetUrl("http://127.0.0.1")
//设置头信息,返回byte类型的body
resp,bodyByte,err:=req.SetHeader("HOST","127.0.0.1").Send().EndByte()
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(bodyByte)
}
3. 设置请求信息(get)
//新建一个http客户端
client:=httpc.NewHttpClient()
//新建一个请求
req:=httpc.NewRequest(client)
req.SetMethod("post").SetUrl("http://127.0.0.1")
//设置头信息
req.SetHeader("HOST","127.0.0.1")
//设置请求信息
resp,body,err:=req.SetParam("client", "httpc").Send().End()
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(body)
}
4. 设置请求信息(post)
//新建一个http客户端
client:=httpc.NewHttpClient()
//新建一个请求
req:=httpc.NewRequest(client)
req.SetMethod("post").SetUrl("http://127.0.0.1")
//设置头信息
req.SetHeader("HOST","127.0.0.1")
//设置请求信息
b:=body.NewUrlEncode()
b.SetData("client","httpc")
resp,body,err:=req.SetBody(b).Send().End()
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(body)
}
//新建一个http客户端
client:=httpc.NewHttpClient()
//新建一个请求
req:=httpc.NewRequest(client)
//设置请求地址和头信息
req.SetUrl("http://127.0.0.1").SetHeader("HOST","127.0.0.1")
//设置请求数据
req.SetData("client", "httpc")
var cookies []*http.Cookie
cookie:=&http.Cookie{Name:"client",Value:"httpc"}
cookies= append(cookies, cookie)
//添加cookie并请求
resp,bodyByte,err:=req.SetCookies(&cookies).Send().End()
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(bodyByte)
}
6. 上传文件
//新建一个http客户端
client:=httpc.NewHttpClient()
//新建一个请求
req:=httpc.NewRequest(client)
req.SetMethod("post").SetUrl("http://127.0.0.1")
//设置上传的文件
b:=body.NewFormData()
b.SetFile("img1","./img.png")
//设置附加参数
b.SetData("client","httpc")
resp,body,err:=req.SetBody(b).Send().End()
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(body)
}
7. 下载文件
//新建一个http客户端
client:=httpc.NewHttpClient()
//新建一个请求
req:=httpc.NewRequest(client)
//请求保存文件
resp,body,err:=req.SetUrl("http://127.0.0.1/1.zip").Send().EndFile("./test/","")
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(body)
}
8. 开启调试
req:=httpc.NewRequest(httpc.NewHttpClient())
req.SetMethod("post").SetUrl("https://127.0.0.1")
req.SetHeader("HOST","127.0.0.1")
b:=body.NewUrlEncode()
b.SetData("client","httpc")
var cookies []*http.Cookie
cookie:=&http.Cookie{Name:"client",Value:"httpc"}
cookies= append(cookies, cookie)
_, _, _ = req.SetBody(b).SetCookies(&cookies).SetDebug(true).Send().End()

⚠ 在实际场景中不建议复用Request,建议每个请求对应一个Request。

高级用法

1. 设置请求超时
//新建http客户端
client:=httpc.NewHttpClient()
//设置请求超时,默认值为30秒
client.SetTimeout(5*time.Second)
//不设置超时
//client.SetTimeout(0)
//新建一个请求
req:=httpc.NewRequest(client)
req.SetMethod("post").SetUrl("http://127.0.0.1")
//设置头信息,返回byte类型的body
resp,bodyByte,err:=req.SetHeader("HOST","127.0.0.1").Send().EndByte()
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(bodyByte)
}
//新建http客户端
client:=httpc.NewHttpClient()
//新建一个cookie管理器,后面所有请求的cookie将保存在这
cookieJar:=httpc.NewCookieJar()
//设置cookie管理器,
client.SetCookieJar(cookieJar)
//新建一个请求
req:=httpc.NewRequest(client)
req.SetMethod("post").SetUrl("http://127.0.0.1")
//设置头信息,返回byte类型的body
resp,bodyByte,err:=req.SetHeader("HOST","127.0.0.1").Send().EndByte()
if err!=nil {
    fmt.Println(err)
}else{
    //从cookie管理器中获取当前访问url保存的cookie
    u, _ := url.Parse("http://127.0.0.1")
    cookies:=cookieJar.Cookies(u)
    fmt.Println(cookies)
    fmt.Println(resp)
    fmt.Println(bodyByte)
}
3. 设置代理
//新建http客户端
client:=httpc.NewHttpClient()
//设置请求代理
client.SetProxy("http://127.0.0.1:10809")
//新建一个请求
req:=httpc.NewRequest(client)
req.SetMethod("post").SetUrl("http://127.0.0.1")
//设置头信息,返回byte类型的body
resp,bodyByte,err:=req.SetHeader("HOST","127.0.0.1").Send().EndByte()
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(bodyByte)
}
4. 设置重定向处理
//新建http客户端
client:=httpc.NewHttpClient()
//设置http客户端重定向处理函数
client.SetRedirect(func(req *http.Request, via []*http.Request) error {
    return http.ErrUseLastResponse
})
//新建一个请求
req:=httpc.NewRequest(client)
req.SetMethod("post").SetUrl("http://127.0.0.1")
//设置头信息,返回byte类型的body
resp,bodyByte,err:=req.SetHeader("HOST","127.0.0.1").Send().EndByte()
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(bodyByte)
}
5. 设置ssl验证
//新建http客户端
client:=httpc.NewHttpClient()
//跳过ssl验证
client.SetSkipVerify(false)
//新建一个请求
req:=httpc.NewRequest(client)
req.SetMethod("post").SetUrl("http://127.0.0.1")
//设置头信息,返回byte类型的body
resp,bodyByte,err:=req.SetHeader("HOST","127.0.0.1").Send().EndByte()
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(bodyByte)
}

License

Apache License Version 2.0 see http://www.apache.org/licenses/LICENSE-2.0.html

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CookieJar

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

func NewCookieJar

func NewCookieJar() *CookieJar

func (*CookieJar) Cookies

func (j *CookieJar) Cookies(u *url.URL) (cookies []*http.Cookie)

func (*CookieJar) SetCookies

func (j *CookieJar) SetCookies(u *url.URL, cookies []*http.Cookie)

type HttpClient

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

HttpClient 封装了 http.Client 与 http.Transport 是构建 Request 的基础客户端对象

func NewHttpClient

func NewHttpClient() *HttpClient

NewHttpClient 创建并返回一个默认配置的 HttpClient 实例

func (*HttpClient) ClearProxy

func (this *HttpClient) ClearProxy() *HttpClient

ClearProxy 清除当前代理配置,使请求不再通过代理服务器发送

func (*HttpClient) CustomizeTransport added in v1.1.2

func (this *HttpClient) CustomizeTransport(f func(tr *http.Transport)) *HttpClient

CustomizeTransport 允许自定义底层 http.Transport 的所有字段

func (*HttpClient) SetCookieJar

func (this *HttpClient) SetCookieJar(j *CookieJar) *HttpClient

SetCookieJar 为客户端设置 CookieJar,用于管理 Cookie CookieJar 会自动存储与发送 Cookie

func (*HttpClient) SetProxy

func (this *HttpClient) SetProxy(proxyUrl string) *HttpClient

SetProxy 设置客户端的代理服务器地址 参数 proxyUrl 为代理地址,如 "http://127.0.0.1:1080" 设置后所有请求将通过代理发送

func (*HttpClient) SetRedirect

func (this *HttpClient) SetRedirect(f func(req *http.Request, via []*http.Request) error) *HttpClient

SetRedirect 设置客户端的重定向策略 调用者需传入一个 CheckRedirect 回调函数,用于处理 3xx 重定向

func (*HttpClient) SetSkipVerify

func (this *HttpClient) SetSkipVerify(isSkipVerify bool) *HttpClient

SetSkipVerify 设置是否跳过 TLS 证书验证 参数 isSkipVerify 为 true 时不验证服务端证书

func (*HttpClient) SetTimeout

func (this *HttpClient) SetTimeout(t time.Duration) *HttpClient

SetTimeout 设置客户端总请求超时时间 参数 t 为超时值,例如 30 * time.Second

type Request

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

Request 封装了 HTTP 请求构建和发送的逻辑

func NewRequest

func NewRequest(client *HttpClient) *Request

NewRequest 创建一个新的 Request 对象,默认使用 GET 方法

func (*Request) End

func (this *Request) End() (*http.Response, string, error)

End 执行请求并返回响应对象、响应内容字符串以及错误

func (*Request) EndByte

func (this *Request) EndByte() (*http.Response, []byte, error)

EndByte 执行请求并返回响应对象、响应内容字节数组以及错误 自动处理 gzip 压缩

func (*Request) EndFile

func (this *Request) EndFile(savePath, saveFileName string) (*http.Response, error)

EndFile 将响应体保存为文件 savePath 为目录路径,saveFileName 可为空,自动根据 URL 获取文件名

func (*Request) GetError

func (this *Request) GetError() error

GetError 返回请求过程中发生的错误

func (*Request) GetResponse

func (this *Request) GetResponse() *http.Response

GetResponse 返回 HTTP 响应对象

func (*Request) Send

func (this *Request) Send(ctxs ...context.Context) *Request

Send 构建并发送 HTTP 请求 可选传入 context,用于控制请求超时或取消

func (*Request) SetBasicAuth

func (this *Request) SetBasicAuth(username, password string) *Request

SetBasicAuth 设置 HTTP Basic Auth 认证

func (*Request) SetBody

func (this *Request) SetBody(body body.Body) *Request

SetBody 设置请求体,实现 body.Body 接口

func (*Request) SetClient

func (this *Request) SetClient(client *HttpClient) *Request

SetClient 替换请求使用的 HttpClient

func (*Request) SetCookies

func (this *Request) SetCookies(cookies *[]*http.Cookie) *Request

SetCookies 设置请求的 Cookie 列表

func (*Request) SetDebug

func (this *Request) SetDebug(d bool) *Request

SetDebug 设置是否开启调试模式 开启后会打印请求方法、URL、头信息、Cookie 和 Body

func (*Request) SetHeader

func (this *Request) SetHeader(name, value string) *Request

SetHeader 添加或修改请求头,支持多次链式调用

func (*Request) SetMethod

func (this *Request) SetMethod(name string) *Request

SetMethod 设置 HTTP 请求方法,自动转为大写

func (*Request) SetParam

func (this *Request) SetParam(name, value string) *Request

SetParam 添加 URL 查询参数,支持多次链式调用

func (*Request) SetUrl

func (this *Request) SetUrl(url string) *Request

SetUrl 设置请求的URL地址

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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