client

package module
v1.0.19 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: Apache-2.0 Imports: 19 Imported by: 2

README

req

项目依赖http请求库

安装

go get -u github.com/szwtdl/req

使用

package main
import (
    "fmt"
	client "github.com/szwtdl/req"
)
func main() {
    // create a new HttpClient
	HttpClient := client.NewHttpClient("https://examples.com")
    // POST请求
    resp, err = HttpClient.DoPost("post", map[string]string{
        "name": "req",
        "age":  "18",
    })
    if err != nil {
        panic(err)
    }
	fmt.Println(string(resp))
	
	// GET请求
	resp, err = HttpClient.DoGet("post")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(resp))
	
}
代理配置
package main
import (
    "fmt"
    client "github.com/szwtdl/req"
)
func main() {
	// create a new HttpClient with proxy
	HttpClient := client.NewHttpClient("https://examples.com")
	// 设置日记
	HttpClient.SetLogger(logger.GetLogger())
	// 设置header
	HttpClient.SetHeader(map[string]string{
		"Content-Type": "application/json",
	})
	// 设置cookies
	HttpClient.SetCookies(map[string]string{
		"JSESSIONID": strings.ToUpper(fmt.Sprintf("%v", md5.Sum([]byte("12345678")))),
	})
	// 配置 HTTP 代理
	err := HttpClient.SetProxy(&ProxyConfig{
		Type:    "http",
		Address: "proxy.example.com:8080",
		Username: "proxyuser",  // 可选
		Password: "proxypass",  // 可选
	})
	// 配置 SOCKS5 代理
	err := HttpClient.SetProxy(&ProxyConfig{
		Type:    "socks5",
		Address: "socks.example.com:1080",
		Username: "proxyuser",  // 可选
		Password: "proxypass",  // 可选
	})
	// 禁用代理
	HttpClient.SetProxy(nil)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToString

func ToString(value interface{}) string

Types

type HttpClient

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

func NewHttpClient

func NewHttpClient(domain string, timeout ...time.Duration) *HttpClient

func (*HttpClient) AddHeader added in v1.0.13

func (h *HttpClient) AddHeader(name, value string)

func (*HttpClient) Close added in v1.0.8

func (h *HttpClient) Close()

func (*HttpClient) DisableJA3 added in v1.0.13

func (h *HttpClient) DisableJA3()

func (*HttpClient) DoGet

func (h *HttpClient) DoGet(path string) ([]byte, error)

func (*HttpClient) DoGetRaw added in v1.0.19

func (h *HttpClient) DoGetRaw(path string) ([]byte, error)

func (*HttpClient) DoPost

func (h *HttpClient) DoPost(path string, postData map[string]string) ([]byte, error)

func (*HttpClient) DoPostAny added in v1.0.6

func (h *HttpClient) DoPostAny(path string, postData interface{}) ([]byte, error)

func (*HttpClient) DoPostMultipart added in v1.0.19

func (h *HttpClient) DoPostMultipart(path string, fields map[string]string) ([]byte, error)

func (*HttpClient) DoPostRaw added in v1.0.6

func (h *HttpClient) DoPostRaw(path, rawBody string) ([]byte, error)

func (*HttpClient) DoPut added in v1.0.16

func (h *HttpClient) DoPut(path string, putData map[string]string) ([]byte, error)

func (*HttpClient) DoPutRaw added in v1.0.16

func (h *HttpClient) DoPutRaw(path string, raw []byte) ([]byte, error)

DoPutRaw 发送原始二进制数据(适用于 OSS 上传)

func (*HttpClient) DownloadFile added in v1.0.13

func (h *HttpClient) DownloadFile(path, savePath string) error

func (*HttpClient) EnableJA3 added in v1.0.11

func (h *HttpClient) EnableJA3(profile string) error

func (*HttpClient) GetCookieValue added in v1.0.13

func (h *HttpClient) GetCookieValue(name string) string

func (*HttpClient) GetCookies

func (h *HttpClient) GetCookies() []*http.Cookie

func (*HttpClient) GetDomain

func (h *HttpClient) GetDomain() string

func (*HttpClient) GetHeader

func (h *HttpClient) GetHeader() map[string]string

func (*HttpClient) GetTimeout added in v1.0.4

func (h *HttpClient) GetTimeout() time.Duration

func (*HttpClient) LogError added in v1.0.1

func (h *HttpClient) LogError(msg string, err error)

func (*HttpClient) LogInfo added in v1.0.1

func (h *HttpClient) LogInfo(msg string, fields ...interface{})

func (*HttpClient) SetCookies

func (h *HttpClient) SetCookies(cookies map[string]string, opts ...bool)

func (*HttpClient) SetDomain

func (h *HttpClient) SetDomain(domain string)

func (*HttpClient) SetHeader

func (h *HttpClient) SetHeader(headers map[string]string)

func (*HttpClient) SetLogger added in v1.0.1

func (h *HttpClient) SetLogger(logger *zap.SugaredLogger)

func (*HttpClient) SetProxy

func (h *HttpClient) SetProxy(cfg *ProxyConfig) error

func (*HttpClient) SetTimeout added in v1.0.4

func (h *HttpClient) SetTimeout(timeout time.Duration)

func (*HttpClient) UploadFile

func (h *HttpClient) UploadFile(path, fieldName, filePath string, extraParams map[string]string) ([]byte, error)

type ProxyConfig

type ProxyConfig struct {
	Type     string
	Address  string
	Username string
	Password string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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