模拟beego登陆
package main import ( "fmt" "time" "io/ioutil" "net/url" "net/http" "net/http/cookiejar" ) func main() { now := time.Now() var err error var resp *http.Response loginAddr := "http://your.web.site/home/login" resp, err = http.Get(loginAddr) if err != nil { fmt.Printf("login failed: %s ", err) panic(err) } fmt.Printf("login status=%s ", resp.Status) doLoginAddr := "http://your.web.site/home/dologin" jar, _ := cookiejar.New(nil) doLoginUrl, _ := url.Parse(doLoginAddr) jar.SetCookies(doLoginUrl, resp.Cookies()) client := http.Client{Jar: jar} resp, err = client.PostForm(doLoginAddr, url.Values{"UserName": {"YourName"}, "UserPwd": {"YourPassword"}}) if err != nil { fmt.Printf("dologin failed: %s ", err) panic(err) } defer resp.Body.Close() body,_ := ioutil.ReadAll(resp.Body) fmt.Printf("dologin status=%s ", resp.Status) fmt.Printf("dologin response body=%s ", string(body)) apiAddr := "http://your.web.site/path" apiResp, apiErr := client.Get(apiAddr) if apiErr != nil { fmt.Printf("api query failed: %s ", apiErr) panic(apiErr) } defer apiResp.Body.Close() body,_ = ioutil.ReadAll(apiResp.Body) fmt.Printf("api query status=%s ", apiResp.Status) fmt.Printf("api query response body=%s ", string(body)) fmt.Printf("cost=%s ", time.Now().Sub(now)) }