Google app engine CookieJar无法捕获传入的cookie

Google app engine CookieJar无法捕获传入的cookie,google-app-engine,go,Google App Engine,Go,我试图去提交一个网页上的表格,让我模拟登录。从这里开始,我尝试使用cookies来保持一个持久会话,以便再调用一个子页面 我能够顺利登录,没有任何问题,我只是在捕获返回服务器设置的cookie时遇到问题。我想知道是不是因为他们的登录脚本进行了多次重定向?(我得到一个输出) 你知道我为什么没抓到饼干被退回吗 以下是我正在使用的代码: import ( "crypto/tls" "fmt" "io/ioutil" "net/http" "net/url"

我试图去提交一个网页上的表格,让我模拟登录。从这里开始,我尝试使用cookies来保持一个持久会话,以便再调用一个子页面

我能够顺利登录,没有任何问题,我只是在捕获返回服务器设置的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 *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\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))

    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\n", resp)

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

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

    defer resp.Body.Close()

    if resp.StatusCode == 200 {
        fmt.Printf("\n\n-----\n")
        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("-----\n\n")
    }
 }
导入(
“加密/tls”
“fmt”
“io/ioutil”
“net/http”
“网络/网址”
“字符串”
“同步”
)
类型Jar结构{
互斥锁
cookies映射[字符串][]*http.Cookie
}
var CookieJar*Jar
func NewJar()*Jar{
jar:=新的(jar)
jar.cookies=make(映射[string][]*http.Cookie)
返回罐
}
//SetCookies在答复中处理Cookie的接收
//给定的URL。它可以选择保存cookies,也可以不保存cookies,具体取决于
//关于jar的策略和实现。
func(jar*jar)SetCookies(u*url.url,cookies[]*http.Cookie){
jar.lk.Lock()
jar.cookies[u.Host]=cookies
jar.lk.Unlock()
}
//Cookies返回Cookies以发送给定URL的请求。
//由实现来遵守标准cookie使用
//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{unsecureskipverify:false},
}
CookieJar=NewJar()
客户端:=&http.client{
运输:tr,,
CheckRedirect:nil,
Jar:CookieJar,
}
返回客户端
}
func登录(){
客户端:=NewClient()
api:=”https://www.statuscake.com/App/"
uri,quo;=url.Parse(“https://www.statuscake.com")
fmt.Printf(“uri:%s\n”,uri)
值:=url.values{}
添加(“用户名”,用户名)
添加(“密码”,密码)
添加(“登录”、“是”)
值。添加(“重定向”,“”)
str:=values.Encode()
req,err:=http.NewRequest(“POST”,api,strings.NewReader(str))
req.Header.Set(“内容类型”、“应用程序/x-www-form-urlencoded”)
请求标题集(“接受”、“文本/html”)
请求标题.Set(“用户代理”、“Mozilla/5.0(Macintosh;英特尔Mac OS X 10_8_4)AppleWebKit/537.36(KHTML,如Gecko)Chrome/29.0.1547.65 Safari/537.36”)
cookies:=CookieJar.cookies(uri)
对于i:=0;i
不确定问题到底出在哪里,但有两点需要注意:

  • 如果您的客户机有一个工作的Jar,则无需手动用cookies填充请求。客户端应该为您透明地处理所有cookie内容:它从响应中提取cookie并存储在jar中,并使用来自jar的cookie填充响应(即使在重定向期间)。这可能会覆盖您手动设置的cookies

  • 不要使用自己的CookieJar实现。饼干处理很糟糕(你可能相信我)。只需结合code.google.com/p/go.net/publicsuffix使用即可