urlx

package module
v0.0.0-...-648a86e Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2022 License: Apache-2.0 Imports: 17 Imported by: 1

README

urlx: http请求的封装, 链式API

Documentation

Index

Constants

View Source
const (
	HeaderAccept         = "Accept"
	HeaderAcceptLanguage = "Accept-Language"
	HeaderUserAgent      = "User-Agent"
	HeaderContentType    = "Content-Type"
	HeaderReferer        = "Referer"
	HeaderCacheControl   = "Cache-Control" // no-cache
	HeaderPragma         = "Pragma"        // no-cache
)
View Source
const (
	HeaderRequestCookie  = "Cookie"     // Request Cookie
	HeaderResponseCookie = "Set-Cookie" // Response Cookie
)
View Source
const (
	MethodGet     = http.MethodGet
	MethodHead    = http.MethodHead
	MethodPost    = http.MethodPost
	MethodPut     = http.MethodPut
	MethodPatch   = http.MethodPatch
	MethodDelete  = http.MethodDelete
	MethodConnect = http.MethodConnect
	MethodOptions = http.MethodOptions
	MethodTrace   = http.MethodTrace
)
View Source
const DownloadTempExt = ".uxdt"
View Source
const Version = "0.0.1"

Variables

View Source
var (
	MacChromeAgent  = UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36")
	MacFirefoxAgent = UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:65.0) Gecko/20100101 Firefox/65.0")
	MacSafariAgent  = UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15")
	MacEdgeAgent    = UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43")

	WindowsChromeAgent = UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36")
	WindowsEdgeAgent   = UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43")
	WindowsIEAgent     = UserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko")

	IOSChromeAgent = UserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) CriOS/31.0.1650.18 Mobile/11B554a Safari/8536.25")
	IOSSafariAgent = UserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12F70 Safari/600.1.4")
	IOSEdgAgent    = UserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/96.0.4664.55")

	AndroidChromeAgent = UserAgent("Mozilla/5.0 (Linux; Android 11; SM-G9910) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36")
	AndroidWebkitAgent = UserAgent("Mozilla/5.0 (Linux; Android 11; SM-G9910) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")
	AndroidEdgeAgent   = UserAgent("Mozilla/5.0 (Linux; Android 11; SM-G9910) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Mobile Safari/537.36 Edge/95.0.1020.55")
)
View Source
var (
	// AcceptChinese 接受中文
	AcceptChinese = AcceptLanguage("zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-TW;q=0.5")

	// AcceptHTML 接受网页浏览器格式
	AcceptHTML = Accept("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")

	// AcceptJSON 接受JSON格式
	AcceptJSON = Accept("application/json")

	// AcceptXML 接受XML格式
	AcceptXML = Accept("application/xml,text/xml")

	// AcceptAny 接受任意格式
	AcceptAny = Accept("*/*")

	// NoCache 无缓存
	NoCache = HeaderOption(func(headers http.Header) {
		headers.Set(HeaderCacheControl, "no-cache")
		headers.Set(HeaderPragma, "no-cache")
	})
)
View Source
var ProcessNil = func(resp *http.Response) error { return nil }

Functions

This section is empty.

Types

type Body

type Body = func() (contentType string, body io.Reader, err error) // 请求提交内容构造方法

一些特定方法的定义

type HeaderOption

type HeaderOption = func(headers http.Header) // 请求头处理

一些特定方法的定义

func Accept

func Accept(accepts ...string) HeaderOption

Accept 接受格式

func AcceptLanguage

func AcceptLanguage(acceptLanguages ...string) HeaderOption

AcceptLanguage 接受语言

func CookieAdd

func CookieAdd(cookies ...*http.Cookie) HeaderOption

CookieAdd 添加Cookie到请求

func CookieAddString

func CookieAddString(cookies ...string) HeaderOption

CookieAddString 添加Cookie到请求

func DefaultUserAgent

func DefaultUserAgent() HeaderOption

func HeaderDel

func HeaderDel(keys ...string) HeaderOption

HeaderDel 删除请求头

func HeaderSet

func HeaderSet(key string, values ...string) HeaderOption

HeaderSet 设置请求头

func Referer

func Referer(referer string) HeaderOption

Referer 引用地址

func UserAgent

func UserAgent(userAgent string) HeaderOption

UserAgent 浏览器代理字符串

type Option

type Option = func(*Request) error // 请求选项

一些特定方法的定义

func CookieEnabled

func CookieEnabled(enabled ...bool) Option

CookieEnabled 开关 Cookie

func Jar

func Jar(jar http.CookieJar) Option

Jar 设置Cookie容器

func TryIdempotent

func TryIdempotent(base time.Duration, maxTimes int) Option

TryIdempotent 幂等重试

func UseClient

func UseClient(client *http.Client) Option

UseClient 使用自定义的HTTP客户端

type Process

type Process = func(resp *http.Response) error // 响应处理器

type ProcessMw

type ProcessMw = func(next Process) Process // 响应预处理器

func Dump

func Dump(w io.Writer, reqBody, respBody bool) ProcessMw

func Progress

func Progress(report ProgressReport, reportInterval ...time.Duration) ProcessMw

Progress 下载进度, reportInterval 报告的时间间隔, 最小1秒, 默认2秒

func ReadCookie

func ReadCookie(read func(cookies []*http.Cookie) error) ProcessMw

ReadCookie 从响应读取Cookie

func Status

func Status(processStatus func(status int) Process) ProcessMw

Status .

type ProgressReport

type ProgressReport = func(total float64, cur float64, speed float64)

ProgressReport 进度报告方法

type Request

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

Request 请求构造

func AndroidEdge

func AndroidEdge(ctx context.Context) *Request

AndroidEdge Android Edge 浏览器

func Browser

func Browser(ctx context.Context) *Request

Browser 浏览器

func Default

func Default(ctx context.Context) *Request

Default 默认的请求器

func MacEdge

func MacEdge(ctx context.Context) *Request

MacEdge Mac Edge 浏览器

func New

func New(ctx context.Context, options ...Option) *Request

New 以一些选项开始初始化请求器

func WindowsEdge

func WindowsEdge(ctx context.Context) *Request

WindowsEdge Windows Edge 浏览器

func (*Request) Body

func (c *Request) Body(body Body) *Request

Body 设置请求提交内容

func (*Request) Bytes

func (c *Request) Bytes() (data []byte, err error)

Bytes 处理响应字节

func (*Request) Download

func (c *Request) Download(fn *string, overwrite ...bool) (err error)

Download 下载到文件

func (*Request) Form

func (c *Request) Form(formBody io.Reader) *Request

func (*Request) FormValues

func (c *Request) FormValues(formBody url.Values) *Request

func (*Request) HeaderWith

func (c *Request) HeaderWith(options ...HeaderOption) *Request

HeaderWith 设置请求头

func (*Request) Method

func (c *Request) Method(method string) *Request

Method 设置请求方法

func (*Request) Process

func (c *Request) Process(process Process) error

Process 处理响应

func (*Request) ProcessWith

func (c *Request) ProcessWith(mws ...ProcessMw) *Request

ProcessWith 在处理之前的预处理

func (*Request) Query

func (c *Request) Query(query string) *Request

Query 设置请求Query参数

func (*Request) TryAt

func (c *Request) TryAt(times ...time.Duration) *Request

TryAt 失败重试,等待休眠时间

func (*Request) Url

func (c *Request) Url(url string) *Request

Url 设置请求链接

func (*Request) UseClient

func (c *Request) UseClient(client *http.Client) *Request

UseClient 使用的客户端定义

func (*Request) With

func (c *Request) With(options ...Option) *Request

With 增加选项

Directories

Path Synopsis
charset module
codec
form Module
html Module
json Module
multipart Module
xml Module
yaml Module
compress module
proxy module
types module

Jump to

Keyboard shortcuts

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