-
Notifications
You must be signed in to change notification settings - Fork 1
/
behavior.go
47 lines (37 loc) · 1000 Bytes
/
behavior.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package http
import (
"net/http"
"net/http/cookiejar"
urlparse "net/url"
)
var DefaultUserAgent = "Mozilla/5.0 UserBehavior"
type UserBehavior struct {
request *http.Request
client *http.Client
}
func NewUserBehavior() *UserBehavior {
var request http.Request
request.Method = "GET"
request.Header = http.Header{}
request.Header.Set("User-Agent", DefaultUserAgent)
jar, _ := cookiejar.New(nil)
return &UserBehavior{
request: &request,
client: &http.Client{Jar: jar},
}
}
func (u *UserBehavior) Header(key, value string) {
u.request.Header.Set(key, value)
}
func (u *UserBehavior) Behave(urlStr string) (*http.Response, error) {
u.request.URL, _ = urlparse.Parse(urlStr)
return u.client.Do(u.request)
}
func (u *UserBehavior) Get(urlStr string) (*http.Response, error) {
return u.Behave(urlStr)
}
func (u *UserBehavior) Post(urlStr string) (*http.Response, error) {
u.request.URL, _ = urlparse.Parse(urlStr)
u.request.Method = "POST"
return u.client.Do(u.request)
}