亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

命名 cookie 不存在

命名 cookie 不存在

Go
海綿寶寶撒 2022-10-10 19:24:29
我正在建立一個網站,該網站將依賴 cookie 來處理各種事情。然后我決定有一個設置cookie然后讀取相同cookie的功能,以查看瀏覽器是否允許cookie。但這失敗了。./views/index.html 中的模板{{define "index"}}template{{end}}主要代碼:package mainimport (    "fmt"    "html/template"    "log"    "net/http"    "strconv"    "time"    "github.com/gorilla/handlers"    "github.com/gorilla/mux")var tmpl *template.Templatefunc main(){    port :=":8088"    router := mux.NewRouter()    router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {        //Set test cookie        cookieName := strconv.FormatInt(time.Now().UnixNano(), 10)        cookieValue := strconv.FormatInt(time.Now().UnixNano(), 10)        fmt.Println("cookieName:" + cookieName)        fmt.Println("cookieValue:" + cookieValue)        cookie := http.Cookie{Name: cookieName, Value: cookieValue, Path: "/"}        http.SetCookie(w, &cookie)        //Get cookies        fmt.Println("Range over cookies")        for _, c := range r.Cookies() {            fmt.Println(c)        }        //Get test cookie by name        c, err := r.Cookie(cookieName)        if err != nil {            fmt.Println("Error: " + err.Error())        } else {            fmt.Println(c.Value)        }        err = tmpl.ExecuteTemplate(w, "index", "")        if err != nil {            http.Error(w, err.Error(), http.StatusInternalServerError)        }    })    var err error    tmpl, err = template.ParseGlob("views/*")    if err != nil {        panic(err.Error())    }    router.PathPrefix("/").HandlerFunc(func(res http.ResponseWriter, req *http.Request) {        http.FileServer(http.Dir("./static/")).ServeHTTP(res, req)    })    fmt.Println("Server running on localhost" + port)    err = http.ListenAndServe(port, handlers.CompressHandler(router))    if err != nil {        log.Fatal(err)    }}這是終端輸出:Server running on localhost:8088cookieName:1636243636497412077cookieValue:1636243636497413613Range over cookiesError: http: named cookie not present任何指向我的問題可能是什么?
查看完整描述

2 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

在將 cookie 發送到客戶端之前,您正在檢查 r.Cookies。您必須發送 cookie,然后如果您想檢查他們的 cookie,請發送第二個請求。在您發送第一個響應后,打開瀏覽器并查看您的 cookie 是否存在會容易得多。



查看完整回答
反對 回復 2022-10-10
?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

Request.Cookie方法從請求 Cookie標頭中獲取 cookie 。

函數http.SetCookie將 Set-Cookie 標頭添加到響應標頭中。您可以使用以下代碼觀察 http.SetCookie 的結果:

 fmt.Println(w.Header()["Set-Cookie"])

當前請求中不存在命名的 cookie,因為 http.SetCookie 不會修改當前請求。

cookie 值的流程是這樣的:

  1. 服務器使用 Set-Cookie 標頭在響應中設置 cookie。

  2. 客戶端將 cookie 存儲在“cookie jar”中。

  3. 客戶端使用 Cookie 請求標頭將 jar 中的匹配 cookie 添加到請求中。

  4. 服務器從請求標頭中獲取 cookie。

試試這個代碼。在瀏覽器中加載頁面并刷新以觀察 cookie 值的流動。

 const cookieName = "example"

    cookieValue := strconv.FormatInt(time.Now().UnixNano(), 10)

    fmt.Printf("Set cookie %s=%s\n", cookieName, cookieValue)


    cookie := http.Cookie{Name: cookieName, Value: cookieValue, Path: "/"}

    http.SetCookie(w, &cookie)


    c, err := r.Cookie(cookieName)

    if err != nil {

        fmt.Printf("Get cookie %s error: %v\n", cookieName, err)

    } else {

        fmt.Printf("Get cookie %s=%s\n", cookieName, c.Value)

    }


查看完整回答
反對 回復 2022-10-10
  • 2 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號