douduan4116 2013-09-17 00:21
浏览 86
已采纳

CookieJar不捕获传入的Cookie

I'm trying to have Go submit a form on a webpage for me to simulate a login. From there I'm trying to use the cookies to keep a persistent session for one more call to a sub-page.

I'm able to successfully do the log in with no issues, I'm just having issues catching the cookies being set by the returning server. I'm wondering if it's because their login script does several redirects? (I am getting an output).

Any ideas why I'm not catching the cookies being returned?

Here is the code I'm using:

 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 *Jar

func 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
", 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))

    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    req.Header.Set("Accept", "text/html")
    req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.65 Safari/537.36")
    cookies := CookieJar.Cookies(uri)
    for i := 0; i < len(cookies); i++ {
        fmt.Printf("Cookie[%d]: %s", i, cookies[i])
        req.AddCookie(cookies[i])
    }

    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Response: %v
", resp)

    fmt.Printf("Response.Cookies: %v
", resp.Cookies())

    cookies = resp.Cookies()
    CookieJar.SetCookies(uri, cookies)

    defer resp.Body.Close()

    if resp.StatusCode == 200 {
        fmt.Printf("

-----
")
        fmt.Println("HTTP Code: ", resp.StatusCode)
        fmt.Println("Response Cookies: ", resp.Cookies())
        fmt.Println("Request Headers: ", req.Header)
        fmt.Println("Request Cookies: ", req.Cookies())
        fmt.Println("Response Headers: ", resp.Header)
        fmt.Printf("-----

")
    }
 }
  • 写回答

1条回答 默认 最新

  • dqq9695 2013-09-17 08:56
    关注

    Not sure where the problem really is, but two notes:

    1. There is no need to populate the request manually with cookies if your Client has a working Jar. The client should handle all cookie stuff transparently for you: It extracts cookies from the response and store in the jar and populates the response with cookies from the jar (even during redirects). Maybe this overwrites your manually set cookies.

    2. Do not roll your own CookieJar implementation. Cookie handling is awful (you may believe me). Just use http://golang.org/pkg/net/http/cookiejar/ maybe in combination with code.google.com/p/go.net/publicsuffix

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn