http

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: GPL-3.0 Imports: 17 Imported by: 3

Documentation

Index

Constants

View Source
const (
	HttpGet        = "GET"
	HttpPost       = "POST"
	HttpPut        = "PUT"
	HttpDelete     = "DELETE"
	DefaultTimeout = time.Duration(30) * time.Second
)

Variables

View Source
var (
	//DialTimeoutSeconds 超时秒数
	DialTimeoutSeconds time.Duration = 30
)

Functions

func CheckRedirect

func CheckRedirect(r *http.Request, via []*http.Request) error

CheckRedirect 检测重定向

func Delete

func Delete(url string) (string, error)

Delete DELETE方式请求URL

func DeleteEx

func DeleteEx(url string, option *HttpOption) (string, error)

DeleteEx DELETE方式请求URL

func DialContext

func DialContext(ctx context.Context, network, addr string) (net.Conn, error)

DialContext 处理连接超时

func DoEx

func DoEx(url, method string, option *HttpOption) (string, error)

DoEx 请求URL

func Get

func Get(url string) (string, error)

Get GET方式请求URL(默认30秒超时)

func GetBody

func GetBody(r *http.Response) (string, error)

GetBody 获取HTTP响应体

func GetCookies

func GetCookies(r *http.Response) ([]*http.Cookie, error)

GetCookies 获取Cookies

func GetEx

func GetEx(url string, option *HttpOption) (string, error)

GetEx GET方式请求URL

func GetQueryString

func GetQueryString(m map[string]string) string

GetQueryString 获取查询字符串

func GetRawParameter

func GetRawParameter(r *http.Request) (string, error)

GetRawParameter 获取原始请求参数

func HTMLDecode

func HTMLDecode(html string) string

HTMLDecode HTML解码

func HttpsBasicAuthorization

func HttpsBasicAuthorization(username, password string) (string, error)

HttpsBasicAuthorization 创建HTTPS认证字符串

func NewHTTPRequest

func NewHTTPRequest(r *Request, body io.Reader) (*http.Request, error)

NewHTTPRequest 创建新的HTTP请求(重写http.NewRequest方法,因为该方法会对URL进行转码)

func NewRawRequest

func NewRawRequest(method, urlStr string, setOpaque bool, body io.Reader) (*http.Request, error)

NewRawRequest 创建新的HTTP请求(重写http.NewRequest方法,因为该方法会对URL进行转码)

func Post

func Post(url, parameter string, contentType ContentType) (string, error)

Post POST方式请求URL

func PostEx

func PostEx(url string, option *HttpOption) (string, error)

PostEx POST方式请求URL

func PrintCookies

func PrintCookies(cookies []*http.Cookie, page string)

PrintCookies 打印Cookies

func Put

func Put(url, parameter string, contentType ContentType) (string, error)

Put PUT方式请求URL

func PutEx

func PutEx(url string, option *HttpOption) (string, error)

PutEx PUT方式请求URL

func URLEncode

func URLEncode(path string) string

URLEncode URL编码

Types

type ContentType

type ContentType int

ContentType ContentType枚举定义

const (
	//TextPlain 普通文本
	TextPlain ContentType = iota

	//TextHTML Html文本
	TextHTML

	//TextXML Xml文本
	TextXML

	//ApplicationJSON Json内容
	ApplicationJSON

	// ApplicationXML Xml内容
	ApplicationXML

	//ApplicationXWwwFormURLEncoded URL编码后的表单内容
	ApplicationXWwwFormURLEncoded

	//MultipartFormData 多分部表单数据
	MultipartFormData

	//MultipartMixed 多分部混合数据
	MultipartMixed

	//ImageGif GIF图片
	ImageGif

	//ImageJpeg JPEG图片
	ImageJpeg

	//UnknownType 未知类型
	UnknownType //请在此之前添加新的定义
)

func (ContentType) String

func (ct ContentType) String() string

获取ContentType的字符串值

func (ContentType) Value

func (ct ContentType) Value() int

Value 获取ContentType的整数值

type HttpOption

type HttpOption struct {
	Parameter        string        //URL参数或表单参数
	ContentType      ContentType   //参数类型
	Timeout          time.Duration //超时
	IgnoreStatusCode bool          //是否忽略HTTP状态
}

HttpOption HTTP选项类

func DefaultHttpOption

func DefaultHttpOption() *HttpOption

DefaultHttpOption 获取新的HttpOption实例

func EnsureHttpOption

func EnsureHttpOption(option *HttpOption) *HttpOption

EnsureHttpOption 确保HttpOption有效

func NewHttpOption

func NewHttpOption(parameter string, contentType ContentType) *HttpOption

NewHttpOption 获取新的HttpOption实例

func NewHttpOptionEx

func NewHttpOptionEx(parameter string, contentType ContentType, timeout time.Duration, ignoreStatusCode bool) *HttpOption

NewHttpOptionEx 获取新的HttpOption实例

func (*HttpOption) GetParameterReader

func (option *HttpOption) GetParameterReader() io.Reader

GetParameterReader 获取参数读取器实例

func (*HttpOption) SetJsonParameter

func (option *HttpOption) SetJsonParameter(data interface{}) error

SetJsonParameter 设置JSON格式参数

type Request

type Request struct {
	URL                string            //Url 要请求的ULR地址
	Method             string            //Method 请求类型:GET,POST,默认:GET
	PostData           map[string]string //PostData 请求要发送的数据
	PostString         string            // 请求要发送的字符串
	SetOpaque          bool              // 是否设置URL不转码
	ContentEncoding    string            // 提交数据:PostData_Text 的编码,默认值:UTF8
	Accept             string            // Accept: application/json
	UserName           string            // 用户账号
	Password           string            // 用户密码
	AcceptEncoding     string            // 声明浏览器支持的编码类型:gzip,deflate,identity 中的一种或多种,逗号分开。默认:gzip,deflate
	AcceptLanguage     string            // 支持的语言
	ContentType        string            // 内容类型,默认值: "application/x-www-form-urlencoded; charset=UTF-8";
	TimeoutSeconds     int               // 请求超时时间(秒为单位),默认:30秒
	WebProxy           string            // 代理服务器IP地址:222.222.222.222:80
	BypassProxyOnLocal bool              // 如果对本地地址不使用代理服务器,则为 true;否则为 false。默认值为 false。
	UserAgent          string            // 浏览器信息,默认:	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
	Referer            string            // 引用页 默认值string.Empty。
	Origin             string            // 来源
	Headers            map[string]string // 头信息
	ErrorInfo          string            // 错误信息
	AllowAutoRedirect  bool              // 是否根据301跳转
	Host               string            // 服务器主机
	Cookies            []*http.Cookie    // Cookies
	CookieJar          *cookiejar.Jar    // CookiesJar
}

Request HTTP请求类

func (*Request) Do

func (r *Request) Do() (string, error)

Do 发送请求

func (*Request) DoEx

func (r *Request) DoEx() (string, []*http.Cookie, error)

DoEx 发送请求

func (*Request) Get

func (r *Request) Get() (string, error)

Get 发送GET请求

func (*Request) GetEx

func (r *Request) GetEx() (string, []*http.Cookie, error)

GetEx 发送GET请求

func (*Request) Post

func (r *Request) Post() (string, error)

Post 发送POST请求

func (*Request) PostEx

func (r *Request) PostEx() (string, []*http.Cookie, error)

PostEx 发送POST请求

Jump to

Keyboard shortcuts

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