gpt4 book ai didi

Golang cookie 的时间戳为空

转载 作者:数据小太阳 更新时间:2023-10-29 03:23:32 25 4
gpt4 key购买 nike

我正在编写一个需要登录网站以获取一些数据的 go 程序。登录过程已完成,但现在我遇到的问题是我无法使用从登录表单获得的 cookie 访问 protected 页面。在检查它们并与我的浏览器获得的那些进行比较后,我注意到我的程序获得带有“空”时间戳的 cookie。有人可以指出,我如何获得具有正确时间戳的 cookie?那太棒了。

这是我的代码:

package main

import (
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strings"
"time"
)

var CookieJar *cookiejar.Jar
var httpClient *http.Client

func dbgPrintCurCookies(CurCookies []*http.Cookie) {
var cookieNum int = len(CurCookies)
log.Printf("cookieNum=%d", cookieNum)
for i := 0; i < cookieNum; i++ {
var curCk *http.Cookie = CurCookies[i]
//log.Printf("curCk.Raw=%s", curCk.Raw)
log.Printf("Cookie [%d]", i)
log.Printf("Name\t=%s", curCk.Name)
log.Printf("Value\t=%s", curCk.Value)
log.Printf("Path\t=%s", curCk.Path)
log.Printf("Domain\t=%s", curCk.Domain)
log.Printf("Expires\t=%s", curCk.Expires)
log.Printf("RawExpires=%s", curCk.RawExpires)
log.Printf("MaxAge\t=%d", curCk.MaxAge)
log.Printf("Secure\t=%t", curCk.Secure)
log.Printf("HttpOnly=%t", curCk.HttpOnly)
log.Printf("Raw\t=%s", curCk.Raw)
log.Printf("Unparsed=%s", curCk.Unparsed)
}
}

func main() {
CookieJar, _ = cookiejar.New(nil)

httpClient := &http.Client{
Jar: CookieJar,
Timeout: 10 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}}

LSFRedirURL := ""
pw := "
us := ""
LoginURL := "login website ?j_password=" + pw + "&j_username=" + us
GetFinalCookieURL := ""



//get first cookie
nextURL := LSFRedirURL
for i := 0; i < 10; i++ {
resp, _ := httpClient.Get(nextURL)
// fmt.Println("StatusCode:", resp.StatusCode)
// fmt.Println(resp.Request.URL)
if resp.StatusCode == 200 {
// fmt.Println("Done!")
break
} else {
nextURL = resp.Header.Get("Location")
}
}

//safe first cookie
url1, _ := url.Parse("first cookie website")
firstCookie := CookieJar.Cookies(url1)[0]
fmt.Println("First Cookie :\\)")

//getting second cookie and params
// var cam []string
var resp *http.Response
nextURL = LoginURL
for i := 0; i < 10; i++ {
resp, _ = httpClient.Post(nextURL, "", nil)
// fmt.Println("StatusCode:", resp.StatusCode)
// fmt.Println(resp.Request.URL)
// cam = append(cam, nextURL)
if resp.StatusCode == 200 {
fmt.Println("Done!")
break
} else {
nextURL = resp.Header.Get("Location")
}
}

//second cookie
url2, _ := url.Parse("website second cookie is from")
secondCookie := CookieJar.Cookies(url2)[0]
fmt.Println("Second Cookie :\\)")

//params
defer resp.Body.Close()
c, _ := ioutil.ReadAll(resp.Body)
data := html.UnescapeString(string(c))
//fmt.Println(data)
getvalue := regexp.MustCompile("value=\".*\"")
values := getvalue.FindAllStringSubmatch(data, -1)
values[0][0] = strings.TrimSuffix(values[0][0], "\"")
values[0][0] = strings.TrimPrefix(values[0][0], "value=\"")
values[1][0] = strings.TrimSuffix(values[1][0], "\"")
values[1][0] = strings.TrimPrefix(values[1][0], "value=\"")

v := url.Values{
"SAMLResponse": {values[1][0]},
"RelayState": {values[0][0]},
}

body := strings.NewReader(v.Encode())

fmt.Println("Values :\\)")

//adding values and cookies to request
req, _ := http.NewRequest("POST", GetFinalCookieURL, body)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.AddCookie(firstCookie)
req.AddCookie(secondCookie)
resp, _ = httpClient.Do(req)

//we got the real cookie
url3, _ := url.Parse("website i get the cookies for")
dbgPrintCurCookies(CookieJar.Cookies(url3))
finalCookie := CookieJar.Cookies(url3)[0]
finalCookie2 := CookieJar.Cookies(url3)[1]



fmt.Println("StatusCode:", resp.StatusCode)
fmt.Println(resp.Request.URL)
nextURL = resp.Header.Get("Location")
fmt.Println(nextURL)

nextURL = "website i need the cookies for"

req, _ = http.NewRequest("GET", nextURL, nil)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.AddCookie(finalCookie)
req.AddCookie(finalCookie2)
resp, _ = httpClient.Do(req)
url3, _ = url.Parse("final cookie website")
dbgPrintCurCookies(CookieJar.Cookies(url3))
fmt.Println(resp.StatusCode)
fmt.Println(resp.Request.URL)

defer resp.Body.Close()
data3, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(data3))

}

这是我得到的 cookie 的示例:

2018/02/10 01:55:48 cookieNum=2
2018/02/10 01:55:48 Cookie [0]
2018/02/10 01:55:48 Name =JSESSIONID
2018/02/10 01:55:48 Value =86E2C361905167A1F64FC45C400649F2.stupo1
2018/02/10 01:55:48 Path =
2018/02/10 01:55:48 Domain =
2018/02/10 01:55:48 Expires =0001-01-01 00:00:00 +0000 UTC
2018/02/10 01:55:48 RawExpires=
2018/02/10 01:55:48 MaxAge =0
2018/02/10 01:55:48 Secure =false
2018/02/10 01:55:48 HttpOnly=false
2018/02/10 01:55:48 Raw =
2018/02/10 01:55:48 Unparsed=[]

编辑:添加了完整的代码。这是浏览器中的 cookie: Cookie in Browser

最佳答案

Cookie 有两种形式:一种是在 Set-Cookie header 中接收,另一种是在 Cookie header 中发送。只有 Set-Cookie header cookie 有过期时间和各种字段,而 Cookie header type cookie 是纯名称,值元组。这就是 cookiejar 返回的内容,因为它包含在 Cookie header 中。

stdlib 的默认 cookie jar 实现不提供提取除名称和值之外的所有 cookie 或字段的机制。如果您需要该信息,请使用任何其他开源 cookie jar 实现。

(请注意,stdlib cookie jar 实现的全部目的是透明地处理 cookie,即使是通过重定向。它不是收集有关传入的 Set-Cookie header 和其中发送的各种值的信息的解决方案。)

关于Golang cookie 的时间戳为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48716283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com