我试图让 Go 在网页上提交一个表单来模拟登录。从那里我尝试使用 cookie 来保持持久会话,以便再调用一次子页面。我能够成功登录而没有任何问题,我只是在捕获返回服务器设置的 cookie 时遇到问题。我想知道是不是因为他们的登录脚本做了几次重定向?(我得到一个输出)。有什么想法为什么我没有发现返回的 cookie?这是我正在使用的代码: import ( "crypto/tls" "fmt" "io/ioutil" "net/http" "net/url" "strings" "sync")type Jar struct { lk sync.Mutex cookies map[string][]*http.Cookie}var CookieJar *Jarfunc NewJar() *Jar { jar := new(Jar) jar.cookies = make(map[string][]*http.Cookie) return jar}// SetCookies handles the receipt of the cookies in a reply for the// given URL. It may or may not choose to save the cookies, depending// on the jar's policy and implementation.func (jar *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) { jar.lk.Lock() jar.cookies[u.Host] = cookies jar.lk.Unlock()}// Cookies returns the cookies to send in a request for the given URL.// It is up to the implementation to honor the standard cookie use// restrictions such as in RFC 6265.func (jar *Jar) Cookies(u *url.URL) []*http.Cookie { return jar.cookies[u.Host]}func NewClient() *http.Client { tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: false},} CookieJar = NewJar() client := &http.Client{ Transport: tr, CheckRedirect: nil, Jar: CookieJar, } return client } func Login() { client := NewClient() api := "https://www.statuscake.com/App/" uri, _ := url.Parse("https://www.statuscake.com") fmt.Printf("uri: %s\n", uri) values := url.Values{} values.Add("username", username) values.Add("password", password) values.Add("Login", "yes") values.Add("redirect", "") str := values.Encode() req, err := http.NewRequest("POST", api, strings.NewReader(str))
1 回答
- 1 回答
- 0 关注
- 201 浏览
添加回答
举报
0/150
提交
取消