TODO: Cookie

This commit is contained in:
huangsimin 2018-10-19 19:16:34 +08:00
parent 3c4a80a8b6
commit 9183e90152
2 changed files with 31 additions and 2 deletions

View File

@ -29,7 +29,7 @@ type BasicAuth struct {
type Session struct { type Session struct {
client *http.Client client *http.Client
transport *http.Transport transport *http.Transport
cookies http.CookieJar cookiejar http.CookieJar
params *Params params *Params
auth *BasicAuth auth *BasicAuth
} }
@ -102,7 +102,7 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
panic(errors.New("error type " + reflect.TypeOf(v).String())) panic(errors.New("error type " + reflect.TypeOf(v).String()))
} }
case ConfigDialTimeout: case ConfigDialTimeout:
// 没时间实现这些小细节
case ConfigProxy: case ConfigProxy:
switch v := values.(type) { switch v := values.(type) {
case string: case string:
@ -142,6 +142,21 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
return return
} }
// SetCookies 设置Cookies 或者添加Cookies
func (ses *Session) SetCookies(cookies interface{}) {
switch v := cookies.(type) {
case http.Cookie:
var req *http.Request
req.AddCookie((*http.Cookie))
ses.cookies.SetCookies(v)
}
}
// DelCookies 设置Cookies 或者添加Cookies
func (ses *Session) DelCookies(cookies interface{}) {
}
// Get 请求 // Get 请求
func (ses *Session) Get(url string) *Workflow { func (ses *Session) Get(url string) *Workflow {
wf := NewWorkflow(ses) wf := NewWorkflow(ses)

View File

@ -1,6 +1,7 @@
package requests package requests
import ( import (
"net/http"
"net/url" "net/url"
"regexp" "regexp"
) )
@ -11,6 +12,7 @@ type Workflow struct {
ParsedURL *url.URL ParsedURL *url.URL
Method string Method string
Body *Params Body *Params
Cookies []*http.Cookie
} }
// NewWorkflow new and init workflow // NewWorkflow new and init workflow
@ -26,6 +28,11 @@ func (wf *Workflow) SwitchSession(ses *Session) {
wf.session = ses wf.session = ses
} }
// AddCookie 添加Cookie
func (wf *Workflow) AddCookie(c *http.Cookie) {
}
// GetStringURL 获取url的string形式 // GetStringURL 获取url的string形式
func (wf *Workflow) GetStringURL() string { func (wf *Workflow) GetStringURL() string {
return wf.ParsedURL.String() return wf.ParsedURL.String()
@ -142,6 +149,13 @@ func (wf *Workflow) SetBodyParams(params ...interface{}) *Workflow {
// Execute 执行 // Execute 执行
func (wf *Workflow) Execute() (*Response, error) { func (wf *Workflow) Execute() (*Response, error) {
req := buildBodyRequest(wf.Method, wf.GetStringURL(), wf.Body) req := buildBodyRequest(wf.Method, wf.GetStringURL(), wf.Body)
if wf.Cookies != nil {
for _, c := range wf.Cookies {
req.AddCookie(c)
}
}
resp, err := wf.session.client.Do(req) resp, err := wf.session.client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err