update
This commit is contained in:
@@ -5,14 +5,16 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.makemake.in/kzkzzzz/mycommon/mylog"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"golang.org/x/time/rate"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.makemake.in/kzkzzzz/mycommon/mylog"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -64,6 +66,7 @@ type Request struct {
|
||||
httpClient *HttpClient
|
||||
contentType string
|
||||
noWaitQps bool
|
||||
cookies []*http.Cookie
|
||||
}
|
||||
|
||||
type HttpClient struct {
|
||||
@@ -79,17 +82,24 @@ func New(opts ...ConfigOpt) *HttpClient {
|
||||
}
|
||||
|
||||
if config.timeout <= 0 {
|
||||
config.timeout = time.Second * 3
|
||||
config.timeout = time.Second * 10
|
||||
}
|
||||
|
||||
if config.transport == nil {
|
||||
config.transport = NewTransport(3072, time.Second*90)
|
||||
}
|
||||
|
||||
//if config.redirectFn == nil {
|
||||
// config.redirectFn = RedirectAllCookies
|
||||
//}
|
||||
|
||||
if config.client == nil {
|
||||
cookieJar, _ := cookiejar.New(nil)
|
||||
|
||||
client := &http.Client{
|
||||
Transport: config.transport,
|
||||
Timeout: config.timeout,
|
||||
Jar: cookieJar,
|
||||
}
|
||||
|
||||
if config.redirectFn != nil {
|
||||
@@ -188,6 +198,18 @@ func (r *Request) SetHeader(k, v string) *Request {
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Request) SetCookies(v ...*http.Cookie) *Request {
|
||||
r.cookies = append(r.cookies, v...)
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Request) SetCookieMap(cookiesMap map[string]*http.Cookie) *Request {
|
||||
for _, v := range cookiesMap {
|
||||
r.cookies = append(r.cookies, v)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Request) SetHeaders(headers map[string]string) *Request {
|
||||
for k, v := range headers {
|
||||
r.SetHeader(k, v)
|
||||
@@ -252,6 +274,10 @@ func (r *Request) Do(method, rawUrl string) (*Response, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, cookie := range r.cookies {
|
||||
req.AddCookie(cookie)
|
||||
}
|
||||
|
||||
query := req.URL.Query()
|
||||
|
||||
for k, v := range r.mapQuery {
|
||||
|
||||
Reference in New Issue
Block a user