zhttp

package module
v0.0.0-...-9549663 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2019 License: MIT Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

zhttp

zhttp 是一个对 net/http 标准库的封装,参考了 python 中著名的 requests 库,解决了 golang 原生 http 库在高并发中 timeout 失效导致线程不能退出的的问题,很多代码及思路来自grequests

Installation

go get github.com/Greyh4t/zhttp

Example

如下为简单示例,更多使用方法请参考源码

package main

import (
	"fmt"
	"time"

	"github.com/Greyh4t/zhttp"
)

func main() {
	//	proxy, _ := url.Parse("http://127.0.0.1:8080")

	resp, err := zhttp.Get("https://www.jd.com/?key1=value1", &zhttp.RequestOptions{
		DialTimeout:        time.Second * 5,
		RequestTimeout:     time.Second * 5,
		DisableRedirect:    true,
		InsecureSkipVerify: true,
		ContentType:        "application/x-www-form-urlencoded",
		Params: map[string]string{
			"key2": "value2",
		},
		RawCookie: "key1=value1; key2=value2",
		//		Proxies: map[string]*url.URL{
		//			"http":  proxy,
		//			"https": proxy,
		//		},
	})
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(resp.StatusCode())
		fmt.Println(len(resp.String()))
		fmt.Println(resp.RawRequest())
		resp.Close()
	}
}

参考

grequests

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDnsCache

func SetDnsCache(resolver *dnscache.Resolver)

Types

type FileUpload

type FileUpload struct {
	// Filename is the name of the file that you wish to upload. We use this to guess the mimetype as well as pass it onto the server
	FileName string

	// FileContents is happy as long as you pass it a io.ReadCloser (which most file use anyways)
	FileContents io.ReadCloser

	// FieldName is form field name
	FieldName string

	// FileMime represents which mimetime should be sent along with the file.
	// When empty, defaults to application/octet-stream
	FileMime string
}

func FileUploadFromDisk

func FileUploadFromDisk(fieldName, filePath string) ([]FileUpload, error)

type RequestOptions

type RequestOptions struct {
	// Data is a map of key values that will eventually convert into the
	// query string of a GET request or the body of a POST request.
	Data map[string]string

	// Params is a map of query strings that may be used within a GET request
	Params map[string]string

	// Files is where you can include files to upload. The use of this data
	// structure is limited to POST requests
	Files []FileUpload

	// JSON can be used when you wish to send JSON within the request body
	JSON interface{}

	// XML can be used if you wish to send XML within the request body
	XML interface{}

	// Headers if you want to add custom HTTP headers to the request,
	// this is your friend
	Headers map[string]string

	// InsecureSkipVerify is a flag that specifies if we should validate the
	// server's TLS certificate. It should be noted that Go's TLS verify mechanism
	// doesn't validate if a certificate has been revoked
	InsecureSkipVerify bool

	// DisableCompression will disable gzip compression on requests
	DisableCompression bool

	// Host allows you to set an arbitrary custom host
	Host string

	// Set custom dns resolution for the current request
	Hosts string

	ContentType string
	UserAgent   string

	// Auth allows you to specify a user name and password that you wish to
	// use when requesting the URL. It will use basic HTTP authentication
	// formatting the username and password in base64 the format is:
	// []string{username, password}
	Auth []string

	// Cookies is an array of `http.Cookie` that allows you to attach
	// cookies to your request
	Cookies map[string]string

	// Proxies is a map in the following format
	// *protocol* => proxy address e.g http => http://127.0.0.1:8080
	Proxies map[string]*url.URL

	// DialTimeout is the maximum amount of time a dial will wait for
	// a connect to complete.
	DialTimeout time.Duration

	// RequestTimeout is the maximum amount of time a whole request(include dial / request / redirect)
	// will wait.
	RequestTimeout time.Duration

	DisableRedirect bool

	RawCookie string
	RawQuery  string
	RawData   string
	IsAjax    bool
}

type Response

type Response struct {
	RawResponse *http.Response
}

func Delete

func Delete(url string, ro *RequestOptions) (*Response, error)

func Get

func Get(url string, ro *RequestOptions) (*Response, error)
func Head(url string, ro *RequestOptions) (*Response, error)

func Options

func Options(url string, ro *RequestOptions) (*Response, error)

func Patch

func Patch(url string, ro *RequestOptions) (*Response, error)

func Post

func Post(url string, ro *RequestOptions) (*Response, error)

func Put

func Put(url string, ro *RequestOptions) (*Response, error)

func Request

func Request(method, url string, ro *RequestOptions) (*Response, error)

func (*Response) Byte

func (self *Response) Byte() []byte

func (*Response) Close

func (self *Response) Close() error

func (*Response) CookiesMap

func (self *Response) CookiesMap() map[string]string

func (*Response) GetHeader

func (self *Response) GetHeader(headerName string) string

func (*Response) HasCookie

func (self *Response) HasCookie(cookieName string) bool

func (*Response) HasCookieAndValue

func (self *Response) HasCookieAndValue(cookieName, cookieVaule string) bool

func (*Response) HasHeader

func (self *Response) HasHeader(headerName string) bool

func (*Response) HasHeaderAndValue

func (self *Response) HasHeaderAndValue(headerName, headerVaule string) bool

func (*Response) HeadersMap

func (self *Response) HeadersMap() map[string]string

func (*Response) RawCookies

func (self *Response) RawCookies() string

func (*Response) RawHeaders

func (self *Response) RawHeaders() string

func (*Response) RawRequest

func (self *Response) RawRequest() string

func (*Response) ReadN

func (self *Response) ReadN(n int64) []byte

func (*Response) StatusCode

func (self *Response) StatusCode() int

func (*Response) String

func (self *Response) String() string

type Session

type Session struct {
	CookieJar http.CookieJar
	Cookies   map[string]string
}

func NewSession

func NewSession(cookies map[string]string) *Session

func (*Session) Delete

func (s *Session) Delete(url string, ro *RequestOptions) (*Response, error)

func (*Session) Get

func (s *Session) Get(url string, ro *RequestOptions) (*Response, error)

func (*Session) Head

func (s *Session) Head(url string, ro *RequestOptions) (*Response, error)

func (*Session) Options

func (s *Session) Options(url string, ro *RequestOptions) (*Response, error)

func (*Session) Patch

func (s *Session) Patch(url string, ro *RequestOptions) (*Response, error)

func (*Session) Post

func (s *Session) Post(url string, ro *RequestOptions) (*Response, error)

func (*Session) Put

func (s *Session) Put(url string, ro *RequestOptions) (*Response, error)

func (*Session) Request

func (s *Session) Request(method string, url string, ro *RequestOptions) (*Response, error)

Jump to

Keyboard shortcuts

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