繁体   English   中英

从站点生成 cookie

[英]Generating a cookie from a site

我试图为其生成 cookie 的站点通过 chrome 以这种格式提供它:

isolate-web-session-id=5f943413-e947-45f9-9fee-78acfc0e8635 ( fake cookie)

我的代码是:

func main() {

    client := &http.Client{}

    /* Authenticate */
    req, err := http.NewRequest("GET", "URL", nil)
    resp, err := client.Do(req)
    if err != nil {
        fmt.Printf("Error : %s", err)
    }
    jar := &myjar{}
    jar.jar = make(map[string][]*http.Cookie)
    client.Jar = jar
    /* Get Details */
    req.URL, _ = url.Parse("URL")
    resp, err = client.Do(req)
    if err != nil {
        fmt.Printf("Error : %s", err)
    }
    fmt.Println(resp)

}

type myjar struct {
    jar map[string][]*http.Cookie
}

func (p *myjar) SetCookies(u *url.URL, cookies []*http.Cookie) {
    fmt.Printf("The URL is : %s\n", u.String())
    fmt.Printf("The cookie being set is : %s\n", cookies)
    p.jar[u.Host] = cookies
}

func (p *myjar) Cookies(u *url.URL) []*http.Cookie {
    fmt.Printf("The URL is : %s\n", u.String())
    fmt.Printf("Cookie being returned is : %s\n", p.jar[u.Host])
    return p.jar[u.Host]
}

回报是:

The URL is : URL
Cookie being returned is : []
&{200  200 HTTP/1.1 1 1 map[Connection:[keep-alive] Content-Language:[zh-HK] Content-Type:[text/html;charset=UTF-8] Date:[Sat, 12 Mar 2022 16:52:05 GMT] Vary:[accept-encoding]] 0xc000388040 -1 [chunked] false true map[] 0xc000140000 0xc000124420}

我希望能够生成一个 cookie,然后我可以在下一个请求中使用它

或者,我可以运行此代码并获得这样的 cookie:

Cookie: 64304afb-250c-4922-8b9d-ad13f8f81deg ( fake cookie)

代码:


    client := http.Client{}
    url := "URL"
    req, _ := http.NewRequest("GET", url, nil)
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    finalcookiec := resp.Request.Response.Cookies()
    if finalcookiec == nil {
        return
    }
    //print finalcookiec

我的最终目标是能够在 go 中生成一个 cookie,然后提交一个 post 请求,这是一个有效的 cookie

这是 web页面的先睹为快

package main

import (
    "fmt"
    "log"
    "net/http"
    "net/http/cookiejar"
)

var client http.Client

func init() {
    // jar has to be initialized, you dont have to nesscesary do it globally
    jar, err := cookiejar.New(nil)
    if err != nil {
        log.Fatalf("Got error while creating cookie jar %s", err.Error())
    }
    client = http.Client{
        Jar: jar,
    }
}

func main() {
    cookie := &http.Cookie{
        Name:   "token",
        Value:  "some_token",
        MaxAge: 300,
    }
    cookie2 := &http.Cookie{
        Name:   "clicked",
        Value:  "true",
        MaxAge: 300,
    }
    
    req, err := http.NewRequest("GET", "http://google.com", nil)
    if err != nil {
        log.Fatalf("Got error %s", err.Error())
    }
    
    // important part, cookies should get printed
    req.AddCookie(cookie)
    req.AddCookie(cookie2)
    for _, c := range req.Cookies() {
        fmt.Println(c)
    }

    // Request is done trough client
    resp, err := client.Do(req)
    if err != nil {
        log.Fatalf("Error occured. Error is: %s", err.Error())
    }
    defer resp.Body.Close()
    fmt.Printf("StatusCode: %d\n", resp.StatusCode)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM