network

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	POST    = "POST"
	GET     = "GET"
	PUT     = "PUT"
	PATCH   = "PATCH"
	DELETE  = "DELETE"
	HEAD    = "HEAD"
	OPTIONS = "OPTIONS"
	TRACE   = "TRACE"
	CONNECT = "CONNECT"
)
View Source
const (
	XwwwFormUrlencoded = "application/x-www-form-urlencoded"
	FormData           = "application/form-data"
)
View Source
const (
	ContentType = "Content-Type"
)

Variables

This section is empty.

Functions

func HTTPGet

func HTTPGet(url string) ([]byte, error)

HTTPGet 发起HTTP Get请求

func HTTPost

func HTTPost(url string, input []byte) ([]byte, error)

HTTPost 发起HTTP Post请求

Types

type BasicAuth added in v1.0.1

type BasicAuth struct {
	Username, Password string
}

BasicAuth 基础认证

type Client

type Client struct {
	HttpClient      *http.Client
	Header          map[string]string
	Version         string
	MaxIdleConns    int
	MaxConnsPerHost int
	Debug           bool
	Auth            BasicAuth
	Cookies         []*http.Cookie
}

Client http 客户端

var HTTP *Client

HTTP Client

func NewClient

func NewClient() *Client

NewClient 实例化http client

func (*Client) ClearCookies added in v1.0.4

func (c *Client) ClearCookies()

ClearCookies 清除cookies

func (*Client) PostForm

func (c *Client) PostForm(url string, form map[string]io.Reader) (*HTTPResponse, error)

PostForm 发起PostForm请求 from-data方式

example

file, _ := os.Open("file.png") //读取文件 defer file.Close()

form := map[string]io.Reader{} //定义form form["source"] = strings.NewReader("post") //字符串 form["file"] = file //文件类型

func (*Client) PostForm2 added in v1.0.5

func (c *Client) PostForm2(url string, values map[string]string) (*HTTPResponse, error)

PostForm2 x-www-form-urlencoded 方式

func (*Client) Request

func (c *Client) Request(action, url string, input []byte, retry int) (*HTTPResponse, error)
Request 发起HTTP请求

* action:POST\GET\PUT\PATCH\DELETE\HEAD\OPTIONS\TRACE\CONNECT * url:请求地址 * input:请求参数 * retry:重试次数,默认0(不重试)

func (*Client) SetCookie added in v1.0.4

func (c *Client) SetCookie(cookie *http.Cookie)

SetCookie 添加cookie

func (*Client) SetDebug

func (c *Client) SetDebug(d bool)

SetDebug 设置debug

func (*Client) SetHTTPTimeout

func (c *Client) SetHTTPTimeout(timeout time.Duration)

SetHTTPTimeout 设置http 超时时间

func (*Client) SetTransport

func (c *Client) SetTransport(transport http.RoundTripper)

SetTransport 设置Transport

type HTTPResponse

type HTTPResponse struct {
	StatusCode         int
	Status             string
	Message            string
	ResponseBodyBytes  []byte
	OriginHTTPResponse *http.Response
}

HTTPResponse http响应

func (*HTTPResponse) ToString

func (r *HTTPResponse) ToString() string

ToString 将http body转化为字符串

Jump to

Keyboard shortcuts

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