httpc

package module
v0.0.0-...-d3f2335 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2021 License: Apache-2.0 Imports: 18 Imported by: 0

README

httpc

GoDoc License

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

特点

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

安装

go get github.com/2654709623/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. 设置请求信息
//新建一个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.SetData("client", "httpc").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)
}
5. 上传文件
//新建一个http客户端
client:=httpc.NewHttpClient()
//新建一个请求
req:=httpc.NewRequest(client)
req.SetMethod("post").SetUrl("http://127.0.0.1")
//设置上传的文件
req.SetFileData("img1","./img.png",true)
//设置附加参数
req.SetFileData("client","httpc",false)
resp,body,err:=req.Send(true).End()
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(body)
}
6. 下载文件
//新建一个http客户端
client:=httpc.NewHttpClient()
//新建一个请求
req:=httpc.NewRequest(client)
//请求保存文件
resp,body,err:=req.SetUrl("http://127.0.0.1/1.zip").Send().EndFile("./test.zip")
if err!=nil {
    fmt.Println(err)
}else{
    fmt.Println(resp)
    fmt.Println(body)
}
7. 开启调试
req:=httpc.NewRequest(httpc.NewHttpClient())
req.SetMethod("post").SetUrl("https://127.0.0.1")
req.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)
_, _, _ = req.SetCookies(&cookies).SetDebug(true).Send().End()

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

高级用法

1. 设置请求超时
//新建http客户端
client:=httpc.NewHttpClient()
//设置请求超时,默认值为30秒
client.SetTimeout(5*time.Second)
//新建一个请求
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.SetTransport(&http.Transport{
    Proxy:                 http.ProxyFromEnvironment,
    MaxIdleConns:          100,
    IdleConnTimeout:       30 * time.Second,
    TLSHandshakeTimeout:   10 * time.Second,
    ExpectContinueTimeout: 1 * time.Second,
    ResponseHeaderTimeout: 10 * time.Second,
})
//新建一个请求
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)
}

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
}

func NewHttpClient

func NewHttpClient() *HttpClient

func (*HttpClient) SetCookieJar

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

func (*HttpClient) SetProxy

func (this *HttpClient) SetProxy(proxyUrl string)

func (*HttpClient) SetRedirect

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

func (*HttpClient) SetSkipVerify

func (this *HttpClient) SetSkipVerify(isSkipVerify bool)

func (*HttpClient) SetTimeout

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

func (*HttpClient) SetTransport

func (this *HttpClient) SetTransport(t *http.Transport) *HttpClient

type Request

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

func NewRequest

func NewRequest(client *HttpClient) *Request

func (*Request) End

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

func (*Request) EndByte

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

func (*Request) EndFile

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

func (*Request) Send

func (this *Request) Send(a ...interface{}) *Request

func (*Request) SetCookies

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

func (*Request) SetData

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

func (*Request) SetDebug

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

func (*Request) SetFileData

func (this *Request) SetFileData(name, value string, isFile bool) *Request

func (*Request) SetHeader

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

func (*Request) SetJsonData

func (this *Request) SetJsonData(s string) *Request

func (*Request) SetMethod

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

func (*Request) SetUrl

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

Jump to

Keyboard shortcuts

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