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 {
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)

View File

@ -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