From 9183e9015217c38c29051faf726e4ea0863efaad Mon Sep 17 00:00:00 2001 From: huangsimin Date: Fri, 19 Oct 2018 19:16:34 +0800 Subject: [PATCH] TODO: Cookie --- requests_method.go | 19 +++++++++++++++++-- workflow.go | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/requests_method.go b/requests_method.go index 40c02e8..6e7b960 100644 --- a/requests_method.go +++ b/requests_method.go @@ -29,7 +29,7 @@ type BasicAuth struct { type Session struct { client *http.Client transport *http.Transport - cookies http.CookieJar + cookiejar http.CookieJar params *Params auth *BasicAuth } @@ -102,7 +102,7 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) { panic(errors.New("error type " + reflect.TypeOf(v).String())) } case ConfigDialTimeout: - + // 没时间实现这些小细节 case ConfigProxy: switch v := values.(type) { case string: @@ -142,6 +142,21 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) { 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 请求 func (ses *Session) Get(url string) *Workflow { wf := NewWorkflow(ses) diff --git a/workflow.go b/workflow.go index 43c959b..aafc60d 100644 --- a/workflow.go +++ b/workflow.go @@ -1,6 +1,7 @@ package requests import ( + "net/http" "net/url" "regexp" ) @@ -11,6 +12,7 @@ type Workflow struct { ParsedURL *url.URL Method string Body *Params + Cookies []*http.Cookie } // NewWorkflow new and init workflow @@ -26,6 +28,11 @@ func (wf *Workflow) SwitchSession(ses *Session) { wf.session = ses } +// AddCookie 添加Cookie +func (wf *Workflow) AddCookie(c *http.Cookie) { + +} + // GetStringURL 获取url的string形式 func (wf *Workflow) GetStringURL() string { return wf.ParsedURL.String() @@ -142,6 +149,13 @@ func (wf *Workflow) SetBodyParams(params ...interface{}) *Workflow { // Execute 执行 func (wf *Workflow) Execute() (*Response, error) { 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) if err != nil { return nil, err