httpclient

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

httpclient

httpclient是模拟http请求包,支持链接复用,使用缓存池性能更好。

Installation

go get -u github.com/tonly18/httpclient

Getting Started

Simple httpclient Example

For simple httpclient, import the global httpclient package github.com/tonly18/httpclient

package main

import (
	"fmt"
	"github.com/tonly18/httpclient"
	"log"
)

func main() {
	httpClient := httpclient.NewClient(&httpclient.Config{})
	for i := 0; i < 10; i++ {
		resp, err := httpClient.Get("http://www.baidu.com", nil).SetHeader(map[string]any{
			"name": "Sam",
			"age":  18,
		}).Do()

		if err != nil {
			log.Fatalf(`%v`, err)
		}
		
		data, _ := resp.GetData()
		fmt.Println("resp:", string(data))

	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient added in v1.0.0

func NewClient(config *Config) *rawClient

Types

type Config

type Config struct {
	Transport     *http.Transport
	CheckRedirect func(req *http.Request, via []*http.Request) error
	Jar           http.CookieJar
	TimeOut       time.Duration
	Debug         bool //debug 模式
}

config struct

type HttpRequest added in v0.0.4

type HttpRequest struct {
	Request *http.Request
	Method  string
}

func NewRequest

func NewRequest(method, rawurl string, body []byte, debug bool) (*HttpRequest, error)

func (*HttpRequest) AddCookie added in v1.0.2

func (r *HttpRequest) AddCookie(cookies []*http.Cookie) *HttpRequest

AddCookie 设置cookie

func (*HttpRequest) Prepare added in v0.0.4

func (r *HttpRequest) Prepare() *http.Request

Prepare 准备request

func (*HttpRequest) SetHeader added in v0.0.4

func (r *HttpRequest) SetHeader(params map[string]any) *HttpRequest

SetHeader 设置头信息

type HttpResponse

type HttpResponse struct {
	Response *http.Response
	Data     []byte
	Close    bool
	Error    error
}

func NewHttpResponse

func NewHttpResponse(response *http.Response) *HttpResponse

func (*HttpResponse) GetCookie added in v1.0.2

func (r *HttpResponse) GetCookie(names ...string) map[string]*http.Cookie

func (*HttpResponse) GetData

func (r *HttpResponse) GetData() ([]byte, error)

func (*HttpResponse) GetDataFromHeader

func (r *HttpResponse) GetDataFromHeader(key string) string

func (*HttpResponse) GetHeaderCode

func (r *HttpResponse) GetHeaderCode() int

Jump to

Keyboard shortcuts

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