我想要一个需要登录网站获取一些数据的go程序。登录过程已完成,但现在我遇到的问题是我无法通过登录表单获取的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
答案 0 :(得分:0)
Cookie有两种形式:一次收到Set-Cookie
标题,另一种收发Cookie
标题。只有Set-Cookie
标头Cookie具有到期时间和各种字段,而Cookie
标头类型Cookie是普通名称,值元组。这就是一个cookiejar返回的内容,因为它包含在Cookie标题中。
stdlib的默认cookie jar实现不提供提取名称和值以外的所有cookie或字段的机制。如果您需要该信息,请使用任何其他开源cookie jar实现。
(请注意,stdlib cookie jar实现的全部目的是透明地处理cookie,即使是通过重定向。它不是收集有关输入Set-Cookie标头及其中发送的各种值的信息的解决方案。)